- 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
How to update a MongoDB document without overwriting the existing one?
To update only a field value, use update() along with $set. This won’t overwrite the existing one. Let us first create a collection with documents −
> db.demo401.insertOne( ... { ... "_id" : 1001, ... "Name" : "Chris", ... "SubjectName" : "MongoDB", ... "Score" : 45 ... } ... ); { "acknowledged" : true, "insertedId" : 1001 }
Display all documents from a collection with the help of find() method −
> db.demo401.find();
This will produce the following output −
{ "_id" : 1001, "Name" : "Chris", "SubjectName" : "MongoDB", "Score" : 45 }
Following is the query to update a document without overwriting the existing one −
> db.demo401.update({_id: 1001}, {$set: {Score:89}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Display all documents from a collection with the help of find() method −
> db.demo401.find();
This will produce the following output −
{ "_id" : 1001, "Name" : "Chris", "SubjectName" : "MongoDB", "Score" : 89 }
- Related Articles
- How to update record in MongoDB without replacing the existing fields?
- How to Copy files or Folder without overwriting existing files?
- How to update an existing document in MongoDB collection using Java?
- Update only a single MongoDB document without deleting any date
- How to update the _id of a MongoDB Document?
- Update two separate arrays in a document with one update call in MongoDB?
- MongoDB findOneAndUpdate() to update a single document
- How do you update a MongoDB document while replacing the entire document?
- MongoDB query to update the nested document?
- MongoDB query to update nested document
- How to add properties from one object into another without overwriting in JavaScript?
- Check for Existing Document in MongoDB?
- How to update or modify the existing documents of a collection in MongoDB?
- Update only a single document in MongoDB
- Remove and update the existing record in MongoDB?

Advertisements