MongoDB - Database, Collection, and Document - GeeksforGeeks

13 Aug, 2021
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. 
In MongoDB, a database contains the collections of documents. One can create multiple databases on the MongoDB server.  
To see how many databases are present in your MongoDB server, write the following statement in the mongo shell:  
show dbs
For Example:  
Here, we freshly started MongoDB so we do not have a database except these three default databases, i.e, admin, config, and local.  
Before creating a database you should first learn about the naming restrictions for databases:  
In MongoDB, the names of the database are case insensitive, but you must always remember that the database names cannot differ only by the case of the characters.
For windows user, MongoDB database names cannot contain any of these following characters: 
/\. "$*:|?
For Unix and Linux users, MongoDB database names cannot contain any of these following characters: 
/\. "$
MongoDB database names cannot contain null characters(in windows, Unix, and Linux systems).
MongoDB database names cannot be empty and must contain less than 64 characters. 
In the mongo shell, you can create a database with the help of the following command:  
use database_name 
This command actually switches you to the new database if the given name does not exist and if the given name exists, then it will switch you to the existing database. Now at this stage, if you use the show command to see the database list where you will find that your new database is not present in that database list because, in MongoDB, the database is actually created when you start entering data in that database. 
For Example:  
Here, we create a new database named GeeksforGeeks using the use command. After creating a database when we check the database list we do not find our database on that list because we do not enter any data in the GeeksforGeeks database. 
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.  
As we know that MongoDB databases are schemaless. So, it is not necessary in a collection that the schema of one document is similar to another document. Or in other words, a single collection contains different types of documents like as shown in the below example where mystudentData collection contain two different types of documents: 
Before creating a collection you should first learn about the naming restrictions for collections: 
Collection name must starts with an underscore or a character.
Collection name does not contain $, empty string, null character and does not begin with system. prefix.
The maximum length of the collection name is 120 bytes(including the database name, dot separator, and the collection name).
After creating database now we create a collection to store documents. The collection is created using the following syntax: 
db.collection_name.insertOne({..})
Here, insertOne() function is used to store single data in the specified collection. And in the curly braces {} we store our data or in other words, it is a document. 
For Example:  
In this example, we create a collection named as the Author and we insert data in it with the help of insertOne() function. Or in other words, {name: “Ankita”} is a document in the Author collection, and in this document, the name is the key or field and “Ankita” is the value of this key or field. After pressing enter we got a message(as shown in the above image) and this message tells us that the data enters successfully (i.e., “acknowledge”: true) and also assigns us an automatically created id. It is the special feature provided by MongoDB that every document provided a unique id and generally, this id is created automatically, but you are allowed to create your own id (must be unique).  
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
}
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.
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. 
singghakshay
MongoDB
Advanced Computer Subject
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
ML | Linear Regression
Decision Tree Introduction with example
Copying Files to and from Docker Containers
System Design Tutorial
Python | Decision tree implementation
ML | Underfitting and Overfitting
Clustering in Machine Learning
Docker - COPY Instruction
KDD Process in Data Mining
How to Run a Python Script using Docker?