 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP 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" }Advertisements
 