📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
✅ Some premium posts are free to read — no account needed. Follow me on Medium to stay updated and support my writing.
🎓 Top 10 Udemy Courses (Huge Discount): Explore My Udemy Courses — Learn through real-time, project-based development.
▶️ Subscribe to My YouTube Channel (172K+ subscribers): Java Guides on YouTube
Show All Databases
> show dbs
Example:> show dbs
local 0.78125GB
test 0.23012GB
MongoDB Create Database
Syntax
use DATABASE_NAME
Example
> use mydb
switched to db mydb
> db
mydb
MongoDB Drop Database
Syntax
db.dropDatabase()
Example
> db.dropDatabase()
{ "dropped" : "mydb", "ok" : 1 }
MongoDB Create Collection
Syntax
db.createCollection(name, options)
Examples
> db.createCollection("mycollection")
{ "ok" : 1 }
> show collections
mycollection
MongoDB Drop Collection
Syntax
db.COLLECTION_NAME.drop()
Example
> show collections
mycollection
posts
> db.mycollection.drop()
true
> show collections
posts
Insert a Single Document
> db.posts.insertOne({
title: 'MongoDB Overview',
description: 'MongoDB is no sql database',
by: 'Java Guides',
url: 'https://www.javaguides.net',
tags: ['mongodb', 'database', 'NoSQL'],
likes: 500
});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e1823977695f4d696a05984")
}
Insert Multiple Documents
db.posts.insertMany([
{
title: 'MongoDB Overview',
description: 'MongoDB is no sql database',
by: 'Java Guides',
url: 'https://javaguides.net',
tags: ['mongodb', 'database', 'NoSQL'],
likes: 100
},
{
title: 'NoSQL Database',
description: "NoSQL database doesn't have tables",
by: 'Java Guides',
url: 'https://javaguides.net',
tags: ['mongodb', 'database', 'NoSQL'],
likes: 20,
comments: [
{
user:'user1',
message: 'My first comment',
dateCreated: new Date(2013,11,10,2,35),
like: 0
}
]
}
]);
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("5e18246f7695f4d696a05985"),
ObjectId("5e18246f7695f4d696a05986")
]
}
MongoDB Query Document
Syntax
> db.COLLECTION_NAME.find()
Example
> db.posts.find( {} );
{ "_id" : ObjectId("5e1840697695f4d696a05987"), "title" : "MongoDB Overview", "description" : "MongoDB is no sql database", "by" : "Java Guides", "url" : "https://javaguides.net", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 100 }
{ "_id" : ObjectId("5e1840697695f4d696a05988"), "title" : "NoSQL Database", "description" : "NoSQL database doesn't have tables", "by" : "Java Guides", "url" : "https://javaguides.net", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 20, "comments" : [ { "user" : "user1", "message" : "My first comment", "dateCreated" : ISODate("2013-12-10T09:35:00Z"), "like" : 0 } ] }
Pretty print Documents
Syntax
> db.posts.find().pretty()
Example
> db.posts.find().pretty()
{
"_id" : ObjectId("5e1840697695f4d696a05987"),
"title" : "MongoDB Overview",
"description" : "MongoDB is no sql database",
"by" : "Java Guides",
"url" : "https://javaguides.net",
"tags" : [
"mongodb",
"database",
"NoSQL"
],
"likes" : 100
}
{
"_id" : ObjectId("5e1840697695f4d696a05988"),
"title" : "NoSQL Database",
"description" : "NoSQL database doesn't have tables",
"by" : "Java Guides",
"url" : "https://javaguides.net",
"tags" : [
"mongodb",
"database",
"NoSQL"
],
"likes" : 20,
"comments" : [
{
"user" : "user1",
"message" : "My first comment",
"dateCreated" : ISODate("2013-12-10T09:35:00Z"),
"like" : 0
}
]
}
Update Documents in a Collection
Update a Single Document
db.posts.updateOne(
{ title : "MongoDB Overview" },
{
$set: { "by": "Ramesh", description: "MongoDB is no sql database and document oriented database" }
}
)
Update Multiple Documents
db.posts.updateMany(
{ "likes": { $lt: 50 } },
{
$set: { "title": "MongoDB NoSQL Database" }
}
);
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
Replace a Document
db.posts.replaceOne(
{ by: "Ramesh" },
{
"title" : "MongoDB Architecture",
"description" : "MongoDB Architecture",
"by" : "Ramesh",
"url" : "https://javaguides.net",
"tags" : [
"mongodb",
"database",
"NoSQL"
],
"likes" : 100,
"lastModified" : ISODate("2020-01-10T09:26:39.709Z")
}
)
Delete All Documents
db.posts.deleteMany({})
Delete Only One Document that Matches a Condition
db.posts.deleteOne( { title: "MongoDB Overview" } )
Delete All Documents that Match a Condition
db.inventory.deleteMany({ by : "Java Guides" })
MongoDB Projection
> db.posts.find({},{"title":1,description:1}).pretty();
{
"_id" : ObjectId("5e184a067695f4d696a0598d"),
"title" : "MongoDB Overview",
"description" : "MongoDB is no sql database"
}
{
"_id" : ObjectId("5e184a067695f4d696a0598e"),
"title" : "NoSQL Database",
"description" : "NoSQL database doesn't have tables"
}
> db.posts.find({},{"title":1}).pretty();
{
"_id" : ObjectId("5e184a067695f4d696a0598d"),
"title" : "MongoDB Overview"
}
{ "_id" : ObjectId("5e184a067695f4d696a0598e"), "title" : "NoSQL Database" }
The Limit() Method
Syntax
> db.COLLECTION_NAME.find().limit(NUMBER)
> db.posts.find({},{"title":1,_id:0}).limit(2);
{ "title" : "MongoDB Overview" }
{ "title" : "NoSQL Database" }
MongoDB Sorting
Syntax
> db.COLLECTION_NAME.find().sort({KEY:1})
Example
> db.posts.find({},{"title":1,_id:0}).sort({"title":-1});
{ "title" : "NoSQL Database" }
{ "title" : "MongoDB Overview" }
{ "title" : "MongoDB CRUD Operations" }
Comments
Post a Comment
Leave Comment