MongoDB Databases and Collections Practice Questions with Solutions

Introduction

MongoDB stores data in a simple hierarchy: databases contain collections, and collections contain documents. Understanding how to create, switch, view, rename, and remove databases and collections is essential before working with larger MongoDB applications. In this chapter, you will practice databases and collections using mongosh commands. Each question is designed to give you hands-on experience with common MongoDB database and collection operations. MongoDB Databases and Collections Practice Questions with Solutions to help you understand the concepts.

Q1. Create and switch to a database

Problem Statement

Create a database named school_management and make it the current database.

MongoDB Command / Query

use school_management

Expected Output

switched to db school_management

Now check the current database:

db

Expected output:

school_management

Explanation

MongoDB uses the use command to switch to a database.

If school_management does not already exist, MongoDB does not immediately create a physical database. The database becomes visible after you store data in it.


Q2. Create a collection explicitly

Problem Statement

Inside the school_management database, create a collection named students.

MongoDB Command / Query

db.createCollection("students")

Expected Output

{ ok: 1 }

Explanation

createCollection() explicitly creates a collection.

Here:

db.createCollection("students")

means that the students collection will be created inside the currently selected database.


Q3. Display all collections in a database

Problem Statement

Check which collections currently exist inside the school_management database.

MongoDB Command / Query

show collections

Expected Output

students

Explanation

show collections is a mongosh helper that lists the collections available in the current database.


Q4. Create another collection

Problem Statement

Create a second collection named teachers in the school_management database.

MongoDB Command / Query

db.createCollection("teachers")

Expected Output

{ ok: 1 }

Now run:

show collections

Expected output:

students
teachers

Explanation

A single MongoDB database can contain multiple collections.

For example:

school_management
├── students
└── teachers

Q5. Insert a document into a collection

Problem Statement

Insert a student document into the students collection.

MongoDB Command / Query

db.students.insertOne({
    name: "Aman",
    age: 15,
    class: 10
})

Expected Output

{
  acknowledged: true,
  insertedId: ObjectId('...')
}

Explanation

The students collection now contains one document.

MongoDB automatically generates an _id value if you do not provide one.


Q6. Create a collection automatically while inserting data

Problem Statement

Create a new collection named courses by inserting a document into it.

MongoDB Command / Query

db.courses.insertOne({
    name: "Python",
    duration: "3 Months"
})

Expected Output

{
  acknowledged: true,
  insertedId: ObjectId('...')
}

Now check the collections:

show collections

Expected output:

courses
students
teachers

Explanation

MongoDB can automatically create a collection when you insert the first document into a collection that does not already exist.

Therefore, you do not always need to use createCollection().


Q7. Check whether a collection exists

Problem Statement

Check whether the students collection exists in the current database.

MongoDB Command / Query

db.getCollectionNames()

Expected Output

[ 'courses', 'students', 'teachers' ]

Explanation

db.getCollectionNames() returns an array containing the names of collections in the current database.

You can use it when you want to programmatically inspect the collections instead of using the show collections shell helper.


Q8. Rename a collection

Problem Statement

Rename the teachers collection to school_teachers.

MongoDB Command / Query

db.teachers.renameCollection("school_teachers")

Expected Output

school_teachers

Now check:

show collections

Expected output:

courses
school_teachers
students

Explanation

renameCollection() changes the name of an existing collection.

The documents inside the collection remain available; only the collection name changes.


Q9. Drop a collection

Problem Statement

Remove the courses collection from the current database.

MongoDB Command / Query

db.courses.drop()

Expected Output

true

Now check:

show collections

Expected output:

school_teachers
students

Explanation

The drop() method permanently removes the collection and the documents stored inside it.

Be careful: dropping a collection is a destructive operation.


Q10. Drop a database

Problem Statement

Remove the entire school_management database after completing the practice.

MongoDB Command / Query

First make sure you are using the correct database:

use school_management

Then run:

db.dropDatabase()

Expected Output

{ ok: 1, dropped: 'school_management' }

Explanation

dropDatabase() removes the current database and all of its collections and documents.

This operation should be used carefully because the deleted data cannot normally be recovered through MongoDB itself.


Key Takeaways

  • A MongoDB database contains collections.
  • A collection contains documents.
  • use databaseName switches to a database.
  • db.createCollection() explicitly creates a collection.
  • show collections displays collections in the current database.
  • MongoDB can automatically create a collection when data is inserted.
  • db.getCollectionNames() returns collection names.
  • renameCollection() changes a collection’s name.
  • drop() removes a collection and its documents.
  • dropDatabase() removes the complete current database.
  • Destructive commands such as drop() and dropDatabase() should be used carefully.

FAQs

1. What is a database in MongoDB?

A MongoDB database is a container that stores collections. Each database can contain multiple collections.

2. What is a collection in MongoDB?

A collection is a group of MongoDB documents. It is roughly comparable to a table in a relational database, although MongoDB collections are more flexible because documents do not have to follow one fixed schema.

3. How do I create a database in MongoDB?

Use the use command:

use school_management

The database becomes persisted when data is stored in it.

4. How do I create a collection in MongoDB?

You can explicitly create one using:

db.createCollection("students")

You can also allow MongoDB to create it automatically when inserting the first document.

5. How do I see all collections in MongoDB?

Use:

show collections

You can also use:

db.getCollectionNames()

6. How do I rename a MongoDB collection?

Use the renameCollection() method:

db.students.renameCollection("school_students")

7. What is the difference between dropping a collection and dropping a database?

drop() removes one collection and its documents, while dropDatabase() removes the entire current database along with all its collections and documents.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top