Document

In MongoDB, the data records are stored as BSON documents. Here, BSON stands for binary representation of JSON documents, although BSON contains more data types as compared to JSON. The document is created using field-value pairs or key-value pairs and the value of the field can be of any BSON type. 

Syntax:  

{ 
field1: value1
field2: value2
....
fieldN: valueN
}

Naming restriction of fields:

Before moving further first you should learn about the naming restrictions for fields: 

  • The field names are of strings.
  • The _id field name is reserved to use as a primary key. And the value of this field must be unique, immutable, and can be of any type other than an array.
  • The field name cannot contain null characters.
  • The top-level field names should not start with a dollar sign ($).

Document Size: The maximum size of the BSON document is 16MB. It ensures that the single document does not use too much amount of RAM or bandwidth(during transmission). If a document contains more data than the specified size, then MongoDB provides a GridFS API to store such type of documents. 

Important Notes –  

  • A single document may contain duplicate fields.
  • MongoDB always saves the order of the fields in the documents except for the _id field (which always comes in the first place) and the renaming of fields may change the order of the fields in the documents.
  • _id Field: In MongoDB, every document store in the collection must contain a unique _id field it is just like a primary key in a relational database. The value of the _id field can be set by the user or by the system (if the user does not create an _id field, then the system will automatically generate an ObjectId for _id field). 
    • When you create a collection MongoDB automatically creates a unique index on the _id field.
    • The _id field is the first field of every document.
    • The value of the _id field can be of any BSON type except arrays.
    • The default value of the _id field is ObjectId.

Example #1:  

Here, name, branch, course, and paid field contain values of string type. amount field contains the value of integer type and _id field is generated by the system. 

Example #2:  

Here, the _id field is created by the user. 

Tip: When you paste data in the functions always use close parenthesis after pasting the data into the function. If you use close parenthesis before pasting data in the function, then you will get an error.
 


MongoDB – Database, Collection, and Document

Databases, collections, documents are important parts of MongoDB without them you are not able to store data on the MongoDB server. A Database contains a collection, and a collection contains documents and the documents contain data, they are related to each other. 

Similar Reads

Database

In MongoDB, a database contains the collections of documents. One can create multiple databases on the MongoDB server....

Collection

Collections are just like tables in relational databases, they also store data, but in the form of documents. A single database is allowed to store multiple collections....

Document

In MongoDB, the data records are stored as BSON documents. Here, BSON stands for binary representation of JSON documents, although BSON contains more data types as compared to JSON. The document is created using field-value pairs or key-value pairs and the value of the field can be of any BSON type....

Contact Us