📘 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.
🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (176K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
db.collection.updateOne(<filter>, <update>, <options>)
db.collection.updateMany(<filter>, <update>, <options>)
db.collection.replaceOne(<filter>, <update>, <options>)
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")
]
}
Update Documents in a Collection
{
<update operator>: { <field1>: <value1>, ... },
<update operator>: { <field2>: <value2>, ... },
...
}
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")
}
)
> 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")
}
);
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
> db.posts.find().pretty();
{
"_id" : ObjectId("5e1840697695f4d696a05987"),
"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")
}
{
"_id" : ObjectId("5e1840697695f4d696a05988"),
"title" : "MongoDB 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
}
]
}
Comments
Post a Comment
Leave Comment