What is Document in MongoDB?

Connect with

Document in mongoDBMongoDB is a document-oriented database. A document in MongoDB is a basic unit or basic building block of data. Here, a mongoDB documents refers to a group of data in a row/record similar to a row/record in a relational database.

“Document” and “JSON document” terminology are interchangeable here.

1. Overview of JSON document in MongoDB

The document is the heart of MongoDB, here the document means JSON document (JSON format of data). In simple language a document is a JSON, it can be simple or nested, or complex it depends on what data format, we need to transfer from one network to another network. if you want to know about JSON document you can visit my other post “JSON Overview“.

In JSON document key will be always UTF-8 character string.

An example of a simple document as:

{
	"name": "Ranjeet",
	"techStacks": ["java", "spring framework", "MongoDB"],
	"contacts": 
          {
           "phone":9811006657, 
           "type":"home" 
          },
	"age": 30,
	"author": true,
	"doj": ISODate("2017-12-10T16:55:40.309Z")
}

Following is the explanation of the data type of keys in the JSON document.

  • name: is a string data type,
  • techStacks: is an array of string data type,
  • contacts: is a map ( key/value) or nested it can be any level
  • age: is a number data type
  • author: is a boolean data type.
  • doj: is a date data type

2. Key points about Key of mongoDB document

  • In JSON document, the key will be always the UTF-8 character string.
  • Key must not contain null character i.e. \o , because this char is used for end of key
  • Key can’t be duplicated, if duplicate key means not a valid document
  • key can not be $ or . (dot) because of a reserved character

3. MongoDB Document is case sensitive

{"age": 30}
{"Age": 30}

In the above, MongoDB JSON document, both the documents are different because name of key in both the document are different string, programmatically by ASCII character value, however, for human meanings are the same. In the first document, the first char is small and in the second mongodb document first character is in the capital. ASCII values of both (i.e. age, Age) are different, that is why both documents are different.

4. JSON Document is type-sensitive

{"age": 30}
{"age": "30"}

In the above JSON documents, keys are the same but data type are different. in the first document value of age is the number and second document value of age is a string, so programmatically data type of both the document are different.

As MongoDB has been written in C++ programming language and you know C++ programming language is case sensitive meaning that “a” and “A” is not equal. because ASCII values of “a” and “A” are different.

You reached this point it means you like this post. Please write your comment which is valuable for me to improve this post. Happy Learning! 🙂


Connect with

Leave a Comment

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