Structure your Data for MongoDB¶
Author: MongoDB Documentation Team
Welcome to the Getting Started guides. This guide will show you how to structure data for MongoDB.
Time required: 15 minutes
What You’ll Need¶
- An idea of what data you’d like to store
Check Your Environment¶
- There are no technical requirements for this guide.
Procedure¶
Define Your Data Set¶
When setting up a data store, your first task is to answer the question: “What data would I like to store and how do the fields relate to each other?”.
This guide uses a hypothetical inventory database to track items and their quantities, sizes, tags, and ratings.
Here is an example of the types of fields you might wish to capture:
name | quantity | size | status | tags | rating |
---|---|---|---|---|---|
journal | 25 | 14x21,cm | A | brown, lined | 9 |
notebook | 50 | 8.5x11,in | A | college-ruled,perforated | 8 |
paper | 100 | 8.5x11,in | D | watercolor | 10 |
planner | 75 | 22.85x30,cm | D | 2019 | 10 |
postcard | 45 | 10x,cm | D | double-sided,white | 2 |
Start Thinking in JSON¶
While a table might seem like a good place to store data, as you can
see from the example above, there are fields in this data set that
require multiple values and would not be easy to search or display if
modeled in a single column (for example – size
and tags
).
In a SQL database you might solve this problem by creating a relational table.
In MongoDB, data is stored as documents. These documents are stored in
MongoDB in JSON
(JavaScript Object Notation) format. JSON documents support
embedded fields, so related data and lists of data can be stored with
the document instead of an external table.
JSON is formatted as name/value pairs. In JSON documents, fieldnames and values are separated by a colon, fieldname and value pairs are separated by commas, and sets of fields are encapsulated in “curly braces” ({}).
If you wanted to begin to model one of the rows above, for example this one:
name | quantity | size | status | tags | rating |
---|---|---|---|---|---|
notebook | 50 | 8.5x11,in | A | college-ruled,perforated | 8 |
You might start with the name
and quantity
fields. In JSON
these fields would look like:
Identify Candidates for Embedded Data and Model Your Data¶
Next you will decide which fields require multiple values. These fields will be candidates for embedded documents or lists/arrays of embedded documents within the document.
For example, in the data above, size
might consist of three
fields:
And some items have multiple ratings, so ratings
might be
represented as a list of documents containing the field scores
:
And you might need to handle multiple tags per item. So you might store them in a list too.
Finally, a JSON document that stores an inventory item might look like this:
This looks very different from the tabular data structure you started with in Step 1.
Note
It’s a JSON standard to quote field names.
Summary¶
Congratulations. You now have an idea of how to structure your data using a JSON document.
What’s Next¶
Next you’ll set up a MongoDB instance. Choose one of the following options:
See Also¶
- The MongoDB manual on Documents
- The MongoDB manual on Data Modeling
- You can define the structure of your data for MongoDB using a JSON schema. See Schema Validation.
- Read more about what MongoDB can do, see Introduction to MongoDB
Note
This guide is intended for new learners of MongoDB. For more in-depth information, see The MongoDB Server Manual.