

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 can we update a record in MongoDB?
To update a record, you need to update on the basis of _id. Let us create a collection with documents −
> db.demo458.insertOne( {_id:101,"Name":"David" } ); { "acknowledged" : true, "insertedId" : 101 } > db.demo458.insertOne( {_id:102,"Name":"Chris" } ); { "acknowledged" : true, "insertedId" : 102 } > db.demo458.insertOne( {_id:103,"Name":"Bob" } ); { "acknowledged" : true, "insertedId" : 103 }
Display all documents from a collection with the help of find() method −
> db.demo458.find();
This will produce the following output −
{ "_id" : 101, "Name" : "David" } { "_id" : 102, "Name" : "Chris" } { "_id" : 103, "Name" : "Bob" }
Following is the query to update a record in MongoDB −
> db.demo458.update({_id:102},{$set:{"Name":"David Miller"}}); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Display all documents from a collection with the help of find() method −
> db.demo458.find();
This will produce the following output −
{ "_id" : 101, "Name" : "David" } { "_id" : 102, "Name" : "David Miller" } { "_id" : 103, "Name" : "Bob" }
- Related Questions & Answers
- Remove and update the existing record in MongoDB?
- How to update record in MongoDB without replacing the existing fields?
- How can we update values in a MySQL table?
- How we can update a Python tuple element value?
- How we can update a Python list element value?
- How can I update child objects in MongoDB?
- How can we update any value in MySQL view as we can update the values in MySQL table?
- How can I update child objects in MongoDB database?
- How can we update columns values on multiple rows with a single MySQL UPDATE statement?
- Can we update MySQL with if condition?
- How to find a record by _id in MongoDB?
- How can we update the values of a collection using LINQ in C#?
- Can we perform MySQL UPDATE and change nothing in a table?
- How can we search a record from MySQL table having a date as a value in it?
- How can we test for the existence of any record in MySQL subquery?
Advertisements