By now, we know that MongoDB is a NoSQL document database and that databases like these have some distinct differences relative to relational databases. In this article, we will dive deeper into the technical aspects of the data that is stored inside of a MongoDB database.
Specifically, we will:
- Dive deeper into how MongoDB stores data via collections and documents
- Gain familiarity with two data file formats: JSON and BSON
- Examine how these data formats are related to data stored in MongoDB
Let’s get started!
Collections and Documents
Recall that MongoDB uses the document model. This means that data stored in a MongoDB database resides in a document within a collection. But what does that actually look like? To help better visualize the document model, let’s imagine we are using MongoDB to run our camera store. Naturally, we need to keep track of purchases, our customers, etc. Let’s break down each layer of the store’s database.
At the highest level, we have our database – an instance of MongoDB that contains all the various data our store needs to operate.
Within this instance of MongoDB are collections. Collections are subsets of our data. So, assuming our database contains three types of data – purchase data, inventory, and customer info – each of these would have its own collection.
Within each collection, we store individual records called documents. These documents all belong to that particular subset of our data. So, for example, within the customer collection, we could store personal information about each of our customers. Every customer would have their own document within the collection.
To summarize, a document is simply a record that stores information about a particular entity. A collection, in turn, is just a group of documents containing similar information. And finally, a MongoDB database is just a number of collections assembled together to store data for a specific use case – in this case running our camera store. This is what the hierarchy would look like visually:
Now that we have a general idea of how data is stored in a MongoDB database, let’s take a closer look at documents and how the information they contain is formatted for flexible storage.