Documents and Data Models


MongoDB is a document database: each record in a MongoDB collection is document. Documents are a structure composed of file and value pairs, similar to JSON objects or other mapping data types.

JSON is a way of representing objects derived from JavaScript; however, many programming environments have support for converting JSON objects into native mapping types. Documents in MongoDB are BSON, which is a binary data format that is like JSON, but includes additional type data.

Fields in documents can hold rich data: other documents, arrays of values or documents. These properties make documents well suited to storing data from applications that use rich objects themselves for data storage. Consider the following document:

  "count": 42,
  "name": "test_document",
  "odds": [ 1, 3, 5, 7, 9 ],
  "reference": {
    "_id": 4,
    "name": "test_sub_document"
  "maps": [
    { "validated": true, "primes": [ 1, 2, 3, 5, 7, 9 ] },
    { "validated": true, "evens": [ 2, 4, 6, 8 ] }

Collections in MongoDB store groups of related documents, analogous to the concept of tables in relational systems: the structure of the documents in a single collection are generally similar. However, MongoDB does not require that all documents have the same structure. For example, the following documents that might all exist in a single collection:

{ "_id": ObjectId(...), "name": "test_1", "active": true, "events": [ ] }
{ "_id": ObjectId(...), "name": "test_20", "active": true, "events": [ { "_id": 1, "type": "review" } { "_id": 2, "type": "inspection" } ] }
{ "_id": ObjectId(...), "name": "test_22", "active": false, "events": [ "checked", "approval"] }
{ "_id": ObjectId(...), "name": "test_34", "active": false, "events": [ 2, 7, 21 ] }
{ "_id": ObjectId(...), "name": "test_3", "active": null }

Data Modeling

Documents have a great ability to capture complex pieces of data, and their flexibility makes designing data models, sometimes schema design, a unique challenge for MongoDB users.

Designing an optimal data model for your application takes time, and decisions about how to represent data in MongoDB depend on a few factors:

  • the amount of data, the natural structure of the data.
  • the queries and modifications your application will perform on the data, including the frequency of these operations and their performance and isolation requirements.
  • the performance characteristics of MongoDB.

The correct data model for any combination of application and data is always a balance of these factors, as determined by appropriate testing and prototyping. MongoDB’s documents and collections support fast prototyping so that you can iterate your data model in response to experiments and evolving application requirements.

Additional Information

See the Data Modeling Section section in the MongoDB Manual for more complete documentation of data modeling considerations in MongoDB.