Creating Database and Collection in MongoDB (original) (raw)

Last Updated : 5 May, 2026

MongoDB is a NoSQL database that stores data in flexible, JSON-like documents, enabling scalable, high-performance data management for modern applications.

MongoDB Database and Collection Setup

Follow these steps to create databases and collections in MongoDB.

Step 1: Create a Database In MongoDB

In MongoDB, a database is created only when data is inserted. The use command switches to a database and creates it automatically when data is added if it does not already exist.

**Syntax:

usedatabase_name

**Example: To create or switch to a database called gfgDB, you would use the following command:

Screenshot-2026-02-03-183600

**Check Existing Databases:

To see all the databases that already exist in your MongoDB instance, use the show dbs command:

show dbs

**Output:

Screenshot-2026-02-03-183703

This command lists all the databases in your MongoDB instance (except empty database), including their respective sizes. To check the current database in use, we can use the db command:

db

This will return the name of the currently selected database, e.g., gfgDB

Step 2: Create a Collection in MongoDB

A collection in MongoDB is a group of schema-flexible documents, similar to a table in a relational databases, created explicitly or automatically on insertion.

**Syntax:

db.createCollection(' collection_name' );

**Example: To create a collection called Student, you would use the following command:

db.createCollection('Student');

For example, running the following command will automatically create the Student collection if it doesn't exist:

db.Student.insertOne({Name: "Noah", age: 23})

**Output:

Screenshot-2026-02-03-184047

**Note:

Step 3: Insert Documents into a Collection in MongoDB

Once a collection exists, add JSON-like documents using insert methods.

**1. insertOne() Method

The insertOne() method in MongoDB is used to insert a single document into a collection.

**Syntax:

db.collection_name.insertOne({ field1: value1, field2: value2, ... });

**Example: Inserts a single document into a new collection.

db.myNewCollection1.insertOne( { name:"geeksforgeeks" } )

**Output:

Screenshot-2026-02-03-184241

**2. insertMany() method

insertMany() method inserts multiple documents in one operation, improving performance over single inserts.

**Syntax:

db.collection_name.insertMany([{ field1: value1, field2: value2, ... }, { field1: value1, field2: value2, ... }, ... ])

**Example: To create myNewCollection2, insert two documents.

db.myNewCollection2.insertMany([{name:"gfg", country:"India"}, {name:"Lucas", age:20}])

**Output:

Screenshot-2026-02-03-184720

Step 4: View Existing Collections

After inserting data, list all collections using the show collections command.

**Syntax:

show collections

**Example: If you're working in the gfgDB database and you want to see all the collections, simply run:

show collections

**Output:

Screenshot-2026-02-03-184833

This shows that the Student collection exists within the gfgDB database.

Best Practices for Managing Databases and Collections

Here are some best practices: