How to Create MongoDB Documents

How to Create MongoDB Documents

In this article, I am going to discuss How to Create MongoDB Documents with Examples. Please read our previous article where we discussed How to Perform CRUD Operations in MongoDB with Examples.

How to Create MongoDB Documents

The data record is stored in MongoDB in the BSON document. A BSON document is known as a binary document or binary representation of a JSON document. A document contains data in the form of key-value pair. In MongoDB, a document is present inside the collection, and a collection is present inside the database.

How to Create MongoDB Documents

Syntax
Following is the syntax to create a MongoDB document:
{
Field: value
Field: Value
Field: Value
……..
}

Important Facts on MongoDB Documents:

Following are some important facts about documents:

  1. The maximum size of a BSON document is 16 MB which ensures that the document does not use much RAM or bandwidth at the time of transmission. If the size of the document is more than 16MB, then such type of document is stored in GridFS API.
  2. A MongoDB document does not contain duplicate fields.
  3. The order of the field is always saved by MongoDB except for the _id field. The _id field is always available in the first place and the order of the remaining fields can be changed.
  4. Every document contains a unique _id field. The value of the _id field can be set by the user or by the system.
  5. If the user does not create an _id field, then MongoDB will automatically create an _id field for that document with a unique value. By default, the _id field value is ObjectId.
  6. _id field value of the can be of any BSON data type accept an array.
  7. In a document, the key must not contain a null character.
  8. Starting from MongoDB 5.0 you are allowed to use $ or dot (.) in the field names.
  9. MongoDB documents are case-sensitive. For example, {“name”: “Suman”}, {“Name”: “Suman”} are two different documents, because the ASCII values of “n” and “N” are different so they are treated as different documents.
Naming Restriction in MongoDB

Following are the naming restrictions for the document fields:

  1. The names of the fields should be in the string.
  2. The_id field is a reserved field and the value of the field should be unique and immutable. It can be of any type except array.
  3. The field name must not contain null characters.
Example- Creating a document without _id field.

Here we insert a document in the mycollection in dotnettutorial database.

db.mycollection.insertOne({name: “Mohit”, age:10, class: 3})

Here, the _id field is automatically generated by MongoDB.

Example- Creating a document without _id field

Example- Creating a document with _id field in MongoDB.

Here we insert a document in the mycollection in dotnettutorial database.

db.mycollection.insertOne({_id: 24344, name: “Sumit”, age:15, class: 8})

Here, we insert our own unique _id field.

Creating a document with _id field in MongoDB

In the next article, I am going to discuss Comparing JSON and BSON in MongoDB. Here, in this article, I try to explain How to Create MongoDB Documents with Examples. I hope you enjoy this How to Create MongoDB Documents with Examples article.

Leave a Reply

Your email address will not be published. Required fields are marked *