Introduction
Welcome back to the MongoDB article series - Part 2. In the previous article, we discussed the concept of MongoDB, including how to install MongoDB. Now, in this article, we will discuss the basic structure of a MongoDB Database including its different data types. We will also learn how to insert, update, and delete data in a MongoDB database. Also, in this article, we will discuss how to create full-featured data storage that is scalable, flexible, and fast. If you want to read the previous articles of these series, then follow the links.
Data in MongoDB database is stored in form of documents. Code examples in this article shows how to create documents, insert, update, and delete documents in a MongoDB database.
Understanding MongoDB
As per the discussion in the introduction articles, we have seen that the data in the MongoDB has a flexible schema. In MongoDB, we didn’t need to declare the new columns of a collection before inserting the data in this database just like the conventional RDBMS concept. So, there are some basic considerations we need to remember at the time of designing a schema in MongoDB.
- We need to design our schema as per our requirements.
- If we use two objects together then we need to combine these two objects as one object.
- We can perform joins at the time of writing data.
- We need to optimize our schema for most frequent data search
- We can perform the most complex data aggregation functions
What are the Documents?
A document is the basic unit of data in MongoDB which is basically equivalent to the row as per the concept of RDBMS systems. Actually, the document is the main heart of the MongoDB.
A document is an ordered set of keys with its associated data or values. Representation of these documents always changes in every programming language, but the basic structure of these types of documents is normally a map, hash, or dictionary. In the case of JavaScript, the document represents an object, just like below.
{“message” : “Hello, MongoDB”}
The above document sample contains a key named “message” with a value of “Hello, MongoDB”. Most of the documents always contain a better complete structure that contains multiple and nested key-value pairs. Normally, a key within a document is mainly string type data. But, instead of that, the key always maintains some notable exceptions.
- Key does not contain any null character. If any null character is used within a key it means it will denote the end of the key.
- There are some special characters (like $ or . etc) which are mainly used for some special circumstances. These characters are basically reserved characters.
MongoDB is actually a type-sensitive and case-sensitive database. So, for this reason, the below two documents are not the same,
{“text”:”Hello”}
{“Text”:”Hello”}
The most important note about the document is that it does not allow any duplicate keys in a single document. For example, the below document is actually an invalid document in the case of MongoDB.
{“message” : “Hello, MongoDB”, “message” : “MongoDB is NoSQL DB”}
What is the Collection?
In MongoDB, a collection can be considered as a Table as per the concept of RDBMS. Basically, the collection is a group of documents. Every collection in MongoDB always has dynamic schemas. This means that documents within a single collection can have any number of different elements or shapes. For example, below two documents can be stored within a single document in MongoDB.
{“message” : “Hello, MongoDB”} {“text”:”Hello”, “count”:10}
As per the above example, both documents do not contain any common key. Also, these two documents also contain different types of values against the key value. So naturally, one question always arises in our mind -- if it means we can store any type of data within a common collection with different types of document structure. Also, do we not require separate collections at all? So, as per the below points, we need to separate and store different structured documents in different collections.
- Maintains the same collection for different types of documents
- It is must faster to obtain the list of collections rather than extract a list of types in a collection.
- If we maintain separate collections for separate types of documents, then grouping a document on the basis of the same kind of elements is just easier to use.
- Also, since MongoDB supports indexes, it is better to maintain separate collections for different types of documents so that we can apply indexes on the basis of document structure.
- In MongoDB, every collection is known by its name. Collection names can be any string value or a UTF-8 string. But still, it contains some restrictions:
- The Empty string (“”) is not a valid collection name.
- Collection names cannot contain a null character.
- Also, we cannot create any collection name starting with the system keyword.
- The collection name which is created by the user does not contain any special characters like ($) in the name.
Data Types of MongoDB
MongoDB actually supports a wide range of data types as values in documents. Actually, MongoDB documents are always thought of as JSON-like data which is a JavaScript object. JSON is basically a simple representation of data. The advantage of JSON is it helps to understand, parse and remember. JSON can accept only null, Boolean, numeric, string, array and object types of data. The below list are the available data types in the MongoDB.
| Data Types | Descriptions |
| String | It is most the commonly used data type. A string must be UTF-8 valid in MongoDB |
| Integer | It is used to store numeric values. It may be either 32 bit or 64 bits. |
| Boolean | It is used to store Boolean data types. It's valued either true or false. |
| Double | It is used to store floating-point values. |
| Arrays | These data types are used to store a list of multiple values in a single key |
| Objects | This data type is used to store embedded data |
| Null | It is used to store null data |
| Date | This data type is used to store the current date or time value in UNIX time format. |







Kumar SourabhPosted Jun 25, 2019, 1:26 AM
Very nice Explanation
Prakash ChasiyaPosted Mar 18, 2019, 7:22 AM
Very nice and simple. Thanks for sharing sir.
Amit Kumar SinghPosted Nov 13, 2018, 4:20 AM
Thanks for sharing Debasis Sir !!!