← All tutorials

GoCareerGo Tutorials

MongoDB Tutorial

Documents, collections, CRUD, aggregation, Atlas and driver connectivity.

Data Modeling in MongoDB

The central modeling decision in MongoDB: embed related data in one document, or reference it across documents (like a foreign key)?

// Embedding — good for 1-to-few, read-together data
{ "_id": 1, "name": "Ada", "address": { "city": "London" } }

// Referencing — good for 1-to-many or shared data
{ "_id": 1, "name": "Ada", "orderIds": [101, 102] }
  • Embed when data is read together and doesn't grow unbounded
  • Reference when data is large, shared across documents, or grows without limit