- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Update only a single MongoDB document without deleting any date
To update only a single document, you need to update a specific data with updateOne(). The updateOne() is used to update a single document within the collection based on the filter.
Let us create a collection with documents −
> db.demo495.insertOne({"FirstName":"Chris","Age":19});{ "acknowledged" : true, "insertedId" : ObjectId("5e84adfeb0f3fa88e22790ca") } > db.demo495.insertOne({"FirstName":"David","Age":21});{ "acknowledged" : true, "insertedId" : ObjectId("5e84ae05b0f3fa88e22790cb") } > db.demo495.insertOne({"FirstName":"Bob","Age":26});{ "acknowledged" : true, "insertedId" : ObjectId("5e84ae0eb0f3fa88e22790cc") } > db.demo495.insertOne({"FirstName":"John","Age":22});{ "acknowledged" : true, "insertedId" : ObjectId("5e84ae15b0f3fa88e22790cd") }
Display all documents from a collection with the help of find() method −
> db.demo495.find();
This will produce the following output −
{ "_id" : ObjectId("5e84adfeb0f3fa88e22790ca"), "FirstName" : "Chris", "Age" : 19 } { "_id" : ObjectId("5e84ae05b0f3fa88e22790cb"), "FirstName" : "David", "Age" : 21 } { "_id" : ObjectId("5e84ae0eb0f3fa88e22790cc"), "FirstName" : "Bob", "Age" : 26 } { "_id" : ObjectId("5e84ae15b0f3fa88e22790cd"), "FirstName" : "John", "Age" : 22 }
Following is the query to use updateOne() and update only a single document −
> db.demo495.updateOne({"FirstName":"David"},{$set: {"Age":23} }); { "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
Display all documents from a collection with the help of find() method −
> db.demo495.find();
This will produce the following output −
{ "_id" : ObjectId("5e84adfeb0f3fa88e22790ca"), "FirstName" : "Chris", "Age" : 19 } { "_id" : ObjectId("5e84ae05b0f3fa88e22790cb"), "FirstName" : "David", "Age" : 23 } { "_id" : ObjectId("5e84ae0eb0f3fa88e22790cc"), "FirstName" : "Bob", "Age" : 26 } { "_id" : ObjectId("5e84ae15b0f3fa88e22790cd"), "FirstName" : "John", "Age" : 22 }
- Related Articles
- Update only a single document in MongoDB
- MongoDB findOneAndUpdate() to update a single document
- Remove only a single document in MongoDB
- Update a single list item of a MongoDB document?
- Update only a specific value in a MongoDB document
- Increment only a single value in MongoDB document?
- Retrieve only a single document specifying a criteria in MongoDB?
- How to update a MongoDB document without overwriting the existing one?
- How to find only a single document satisfying the criteria in MongoDB?
- MongoDB findById returning a list of documents instead of a single result? How to get only a single document?
- MongoDB query to update nested document
- MongoDB query to update only a single item from voting (up and down) records?
- How do you update a MongoDB document while replacing the entire document?
- Update only a single column value in MySQL
- A single MySQL query to update only specific records in a range without updating the entire column

Advertisements