MongoDB provides several commands to create databases, manage collections, insert documents, retrieve records, update data, and delete documents. In this lesson, we will learn the most commonly used MongoDB commands with practical examples.
The show dbs command displays the databases
that contain stored data.
show dbs
Example output:
admin 40.00 KiB
config 72.00 KiB
local 80.00 KiB
school 40.00 KiB
Note: A database may not appear in
show dbs until data has been stored in it.
Use the use command to switch to a database.
If the database does not exist, MongoDB selects it and
creates it when data is first stored.
use school
Output:
switched to db school
Check the current database:
db
A collection stores documents. Use
createCollection() to create one explicitly.
db.createCollection("students")
List the collections in the current database:
show collections
The insertOne() method inserts one document
into a collection.
db.students.insertOne({
name: "Rahul",
age: 20,
course: "ADCA"
})
To insert multiple documents, use
insertMany().
db.students.insertMany([
{
name: "Amit",
age: 21,
course: "Tally Prime"
},
{
name: "Priya",
age: 19,
course: "Web Development"
},
{
name: "Neha",
age: 22,
course: "Python"
}
])
The find() method retrieves documents from
a collection.
Display all students:
db.students.find()
Display students in a readable format:
db.students.find().pretty()
Find students whose course is ADCA:
db.students.find({
course: "ADCA"
})
Find one matching document:
db.students.findOne({
name: "Rahul"
})
Use updateOne() to update the first document
matching a condition.
db.students.updateOne(
{ name: "Rahul" },
{ $set: { age: 21 } }
)
The $set operator changes the specified field
without replacing the entire document.
Use updateMany() to update every matching
document.
db.students.updateMany(
{ course: "ADCA" },
{ $set: { status: "Active" } }
)
The deleteOne() method deletes the first
matching document.
db.students.deleteOne({
name: "Rahul"
})
The deleteMany() method deletes all documents
matching the given condition.
db.students.deleteMany({
status: "Inactive"
})
Warning: Always check your filter before running a delete command. An incorrect filter can remove more documents than intended.
Use countDocuments() to count documents
matching a condition.
Count all student documents:
db.students.countDocuments({})
Count students enrolled in ADCA:
db.students.countDocuments({
course: "ADCA"
})
Use sort() to arrange documents and
limit() to restrict the number of results.
Sort students by age in ascending order:
db.students.find().sort({
age: 1
})
Sort students by age in descending order:
db.students.find().sort({
age: -1
})
Display only the first two documents:
db.students.find().limit(2)
The drop() method removes a collection.
db.students.drop()
To remove the current database, use:
db.dropDatabase()
Important: These commands permanently remove database objects or data. Use them only in a practice database when you are learning.
| Command | Purpose |
|---|---|
show dbs |
Display databases |
use school |
Select a database |
show collections |
List collections |
db.createCollection() |
Create a collection |
insertOne() |
Insert one document |
find() |
Retrieve documents |
updateOne() |
Update one matching document |
deleteOne() |
Delete one matching document |
countDocuments() |
Count matching documents |
drop() |
Remove a collection |
db.dropDatabase() |
Remove the current database |
Complete these tasks in MongoDB Shell (mongosh):
school.students.1. Which command displays databases?
2. Which method inserts one document?
3. Which method counts matching documents?