Introduction
MongoDB is a popular NoSQL database used to store data in flexible, document-based structures. Instead of storing information in traditional rows and columns, MongoDB stores data as documents similar to JSON. In this chapter, you will practice basic MongoDB concepts such as databases, collections, documents, BSON, and simple commands. These examples are designed for beginners who are starting MongoDB from zero. MongoDB Introduction Practice Questions with Solutions to help you understand the concepts.
1. Check Whether MongoDB Is Available
Problem Statement
You have installed MongoDB and want to check whether the MongoDB Shell (mongosh) is available on your computer.
MongoDB Command / Query
mongosh --version
Expected Output
2.x.x
The exact version number may be different on your computer.
Explanation
The mongosh --version command displays the installed MongoDB Shell version.
If a version number appears, mongosh is available.
2. Connect to MongoDB
Problem Statement
Connect to a MongoDB server running on your local computer.
MongoDB Command / Query
mongosh
Expected Output
Connecting to: mongodb://127.0.0.1:27017/
...
test>
Explanation
Running mongosh starts the MongoDB Shell and connects to the MongoDB server running locally.
The prompt:
test>
means you are currently working with the test database.
3. Display the Current Database
Problem Statement
After opening MongoDB Shell, find out which database you are currently using.
MongoDB Command / Query
db
Expected Output
test
Explanation
The db command displays the name of the current database.
MongoDB commonly starts you in the test database when you open the shell without selecting another database.
4. Create or Switch to a Database
Problem Statement
Create or switch to a database named school.
MongoDB Command / Query
use school
Expected Output
switched to db school
Explanation
The use command selects a database.
use school
If school already exists, MongoDB switches to it.
If it does not exist, MongoDB selects it, but the database is generally created only when you store data in it.
You can check the current database with:
db
Output:
school
5. Show Available Databases
Problem Statement
You want to see the databases currently available in your MongoDB server.
MongoDB Command / Query
show dbs
Expected Output
admin 40.00 KiB
config 72.00 KiB
local 72.00 KiB
school 40.00 KiB
The exact sizes and database names can differ.
Explanation
The show dbs command displays databases that currently contain stored data and that the connected user can access.
MongoDB also has built-in databases such as admin, config, and local.
6. Create a Collection
Problem Statement
Create a collection named students inside the current school database.
MongoDB Command / Query
db.createCollection("students")
Expected Output
{ ok: 1 }
Explanation
A MongoDB collection is similar to a table in a relational database.
The command:
db.createCollection("students")
creates a collection called students.
The collection belongs to the currently selected database.
You can check the collections with:
show collections
Expected output:
students
7. Insert Your First MongoDB Document
Problem Statement
Insert one student into the students collection with the name Rahul, age 14, and course Python.
MongoDB Command / Query
db.students.insertOne({
name: "Rahul",
age: 14,
course: "Python"
})
Expected Output
{
acknowledged: true,
insertedId: ObjectId('...')
}
Explanation
insertOne() is used to insert a single document into a collection.
The document contains three fields:
{
name: "Rahul",
age: 14,
course: "Python"
}
MongoDB automatically generates a unique _id value if you do not provide one.
The _id helps MongoDB uniquely identify the document.
8. Display Documents from a Collection
Problem Statement
Display all students stored inside the students collection.
MongoDB Command / Query
db.students.find()
Expected Output
[
{
_id: ObjectId('...'),
name: 'Rahul',
age: 14,
course: 'Python'
}
]
Explanation
The find() method retrieves documents from a collection.
Here:
db.students.find()
means:
db→ current databasestudents→ collectionfind()→ retrieve documents
MongoDB displays the automatically generated _id along with the fields you inserted.
9. Insert Multiple Documents
Problem Statement
Add three more students to the students collection.
MongoDB Command / Query
db.students.insertMany([
{
name: "Priya",
age: 15,
course: "Java"
},
{
name: "Aman",
age: 16,
course: "JavaScript"
},
{
name: "Neha",
age: 14,
course: "MongoDB"
}
])
Expected Output
{
acknowledged: true,
insertedIds: {
'0': ObjectId('...'),
'1': ObjectId('...'),
'2': ObjectId('...')
}
}
Explanation
insertMany() allows you to insert multiple documents at the same time.
Here, three student documents are inserted into the students collection.
You can now check the collection:
db.students.find()
You should see four students in total if you also completed Question 7.
10. Find a Specific Student
Problem Statement
Find the student whose name is Neha.
MongoDB Command / Query
db.students.findOne({
name: "Neha"
})
Expected Output
{
_id: ObjectId('...'),
name: 'Neha',
age: 14,
course: 'MongoDB'
}
Explanation
findOne() searches for a single document that matches the specified condition.
Here:
{
name: "Neha"
}
is the search condition.
MongoDB looks inside the students collection and returns the first matching document.
The important difference is:
find()
is commonly used to retrieve matching documents, while:
findOne()
returns a single matching document.
Key Takeaways
- MongoDB is a NoSQL, document-oriented database.
- MongoDB stores data in documents.
- Documents are grouped inside collections.
- Collections are stored inside databases.
mongoshis the MongoDB Shell.dbshows the current database.use databaseNameswitches to a database.show dbsdisplays available databases.show collectionsdisplays collections in the current database.insertOne()inserts one document.insertMany()inserts multiple documents.find()retrieves documents.findOne()retrieves one matching document.- MongoDB automatically creates an
_idfor a document when one is not provided.
FAQs
1. What is MongoDB?
MongoDB is a NoSQL database that stores data as flexible documents instead of traditional rows and columns.
2. Is MongoDB a programming language?
No. MongoDB is a database system, not a programming language. You can use programming languages such as JavaScript, Python, Java, C#, and others to work with MongoDB.
3. What is a document in MongoDB?
A document is an individual record stored in MongoDB. It contains fields and values and is similar in structure to a JSON object.
4. What is a collection in MongoDB?
A collection is a group of MongoDB documents. It is broadly similar to a table in a relational database.
5. What is BSON in MongoDB?
BSON stands for Binary JSON. MongoDB uses BSON to store documents internally. BSON supports additional data types that are not available in standard JSON, such as ObjectId and date types.
6. What is the difference between find() and findOne() in MongoDB?
find() is used to retrieve matching documents, while findOne() returns a single matching document.
7. Can MongoDB create a database automatically?
Yes. MongoDB can create a database when you select a new database and then store data in it. Simply running use databaseName does not necessarily create a visible database until data is stored.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
