Open In App

MongoDB - Database, Collection, and Document

Last Updated : 28 Aug, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

MongoDB is a popular NoSQL database that offers a flexible, scalable, and high-performance way to store data. In MongoDB, databases, collections, and documents are the important building blocks for data storage and management.

  • Multiple Databases: MongoDB allows you to create multiple databases on a single server. Each database is logically isolated from others.
  • Default Databases: When you start MongoDB, three default databases are created: admin, local, and config . These are used for internal purposes.
  • Database Creation: Databases are created when you insert data into them. You can create or switch to a database using the following command:
mongodb_database
MongoDb Database


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, you will find that your new database is not present in that database list because, in MongoDB, the database is actually created when we start entering data in that database. 

4. View Database: To see how many databases are present in your MongoDB server, write the following statement in the mongo shell:

show dbs

Here, we freshly started MongoDB, so we do not have a database except for these three default databases, i.e., admin, config, and local.

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

Naming Restriction for Database:

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

  • Database names must be case-insensitive.
  • The names cannot contain special characters such as /, ., $, *, |, etc.
  • 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. 

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: 

/\. "$

Example:

use LibraryDB  # Switch to the LibraryDB database

Collection in MongoDB

A Collection in MongoDB is similar to a table in relational databases. It holds a group of documents and is a part of a database. Collections provide structure to data, but like the rest of MongoDB, they are schema-less.

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: 

Multiple Collections per Database

A single database can contain multiple collections, each storing different types of documents.

Naming Restrictions for Collection:

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

  • Collection name must starts with an underscore (`_`) or a letter (a-z or A-Z)
  • Collection name should not start with a number, and does not contain $, empty string, null character and does not begin with prefix `system.` as this is reserved for MongoDB system collections.
  • The maximum length of the collection name is 120 bytes(including the database name, dot separator, and the collection name).

Example:

db.books.insertOne({ title: "Learn MongoDB", author: "Jane Doe", year: 2023 })

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

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
}

Document Structure:

A document in MongoDB is a flexible data structure made up of field-value pairs. For instance:

{
title: "MongoDB Basics",
author: "John Doe",
year: 2025
}

Naming restriction for Document Fields:

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

  • Fields in documents must be named with 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. 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

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

  • Automatic ObjectId Generation: When you don't define the _id field, MongoDB generates a unique ObjectId by default.
  • Custom _id: You can set the _id field to a custom value, provided it is unique within the collection.

Example with ObjectId:

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 with Custom _id: 

Here, the _id field is created by the user. 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.

Key Differences Between Databases, Collections, and Documents

  • Database: A container for collections, providing structure and logical isolation for data.
  • Collection: A group of documents within a database, similar to a table in relational databases.
  • Document: A single data record within a collection, stored as a BSON object.
MongoDB-database-colection

Practical Example: Creating a Database, Collection, and Document

Here’s how you can create a database, collection, and document in MongoDB step by step:

1. Create or Switch to a Database

use LibraryDB

2. Create a Collection and Insert a Document

db.books.insertOne({
title: "MongoDB for Beginners",
author: "Alice Johnson",
year: 2023
})

3. Verify the Insertion

db.books.find()

This will display the document stored in the books collection within the LibraryDB database.

MongoDB Indexes and the _id Field

Automatic Index on _id: MongoDB automatically creates a unique index on the _id field for every collection. This index helps MongoDB quickly find documents based on their unique identifier.


Explore