🎓 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 (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
In this post, we will see how to update an existing document in MongoDB.
This page uses the following mongo shell methods:
db.collection.updateOne(<filter>, <update>, <options>)
db.collection.updateMany(<filter>, <update>, <options>)
db.collection.replaceOne(<filter>, <update>, <options>)
The examples on this page use the posts collection. To create and/or populate the posts collection, run the following:
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
To update a document, MongoDB provides update operators, such as $set, to modify field values.
To use the update operators, pass to the update methods an update document of the form:
{
<update operator>: { <field1>: <value1>, ... },
<update operator>: { <field2>: <value2>, ... },
...
}
Some update operators, such as $set, will create the field if the field does not exist.
Update a Single Document
The following example uses db.collection.updateOne() method on the posts collection to update the first document where title equals "MongoDB Overview":
db.posts.updateOne(
{ title : "MongoDB Overview" },
{
$set: { "by": "Ramesh", description: "MongoDB is no sql database and document oriented database" }
}
)
The update operation uses the $set operator to update the value of the by field to "Ramesh" and the value of the description field to "MongoDB is no sql database and document oriented database".
Update Multiple Documents
The following example uses the db.collection.updateMany() method on the posts collection to update all documents where by field equal to ""https://javaguides.net":
db.posts.updateMany(
{ "likes": { $lt: 50 } },
{
$set: { "title": "MongoDB NoSQL Database" }
}
);
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
Replace a Document
To replace the entire content of a document except for the _id field, pass an entirely new document as the second argument to db.collection.replaceOne().
The following example replaces the first document from the inventory collection where by: "Ramesh":
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")
}
)
Here is the complete example:
> 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
}
]
}
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
🆕 High-Demand
80–90% OFF
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
🆕 High-Demand
80–90% OFF
ChatGPT + Generative AI + Prompt Engineering for Beginners
🚀 Trending Now
80–90% OFF
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
🌟 Top Rated
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
🌟 Top Rated
80–90% OFF
Testing Spring Boot Application with JUnit and Mockito
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Master Spring Data JPA with Hibernate
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
🎓 Student Favorite
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Comments
Post a Comment
Leave Comment