MongoDB Document Format

Shahriar Shovon

Supported OS

  • All


Share this post with your friends

In my previous post, I talked about what MongoDB is. If you haven't read my previous post, you can read it at Introduction to MongoDB In this post I am going to talk about MongoDB document format. In SQL databases a table is made up of many rows, and each rows have many columns. Each column represents a data of a specific type. But in MongoDB, a row is represented as a document, each column type is represented as a property of the document, and the value of the column is represented as the value of the property. Let's see an example, then everything should be clear.

Table rows vs Document

A MySQL Table looks like this: +----------+------------+-----------+ | id | name | age | +----------+------------+-----------+ | 1 | Shovon | 21 | +----------+------------+-----------+ A MongoDB Document looks like this: { _id: 1, name: "Shovon", age: 21 } Do you see the differences? Did you notice how the key and value pairs are represented in a document?

The _id key

A MongoDB document must have _id key. _id key must be unique throught the document. When you create a new document, MongoDB automatically inserts an unique _id for you if you don't do so yourself. This _id is used to uniquely identify the document and in replication. I will talk about replication later. Now you know the structure of a MongoDB document. That's it for this post.