MongoDB Database, Collection, and Document (original) (raw)

Last Updated : 5 May, 2026

MongoDB is a NoSQL database that stores data in a flexible structure. A database is like a container for data, a collection is a group of related records, and a document is a single record stored in JSON-like format.

mongodb_database

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.

Screenshot-2026-02-04-181629

The use GeeksforGeeks command creates and switches to a database, but it does not appear in the database list until at least one document is inserted.

Screenshot-2026-02-04-181735

Naming Restriction for Database

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

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

Screenshot-2026-02-10-143547

Naming Restrictions for Collection

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

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

Screenshot-2026-02-04-182414

Document in MongoDB

MongoDB stores data as BSON documents, a binary form of JSON that supports additional data types.

**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:

Document Size

_id Field in MongoDB

Every document must have a unique _id field, acting like a primary key.

Example with ObjectId

Screenshot-2026-02-10-123502

The name, branch, course, and paid fields are strings, the amount field is an integer, and the _id field is system-generated.

Example with Custom _id

Screenshot-2026-02-10-123911

Differences Between Databases, Collections, and Documents

MongoDB organizes data hierarchically into databases, collections, and documents, each serving a distinct purpose.

database

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.

**Note: 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.