Open In App

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. 



Database

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

View Database:

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.  

Naming Restriction for Database:

Before creating a database you should first learn about the naming restrictions for databases:  

/\. "$*:|?
/\. "$

Creating Database:

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. 

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.  

Schemaless:

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: 

Naming Restrictions for Collection:

Before creating a collection you should first learn about the naming restrictions for collections: 

Creating collection:

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).  

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: 

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 –  

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.
 

Article Tags :