
- 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
MongoDB query to update a MongoDB row with only the objectid
Use UPDATE to update and SET to set the new values. Let us create a collection with documents −
> db.demo34.insertOne({"StudentFirstName":"Chris","StudentAge":24}); { "acknowledged" : true, "insertedId" : ObjectId("5e1758b4cfb11e5c34d898cd") } > db.demo34.insertOne({"StudentFirstName":"David","StudentAge":23}); { "acknowledged" : true, "insertedId" : ObjectId("5e1758bdcfb11e5c34d898ce") } > db.demo34.insertOne({"StudentFirstName":"Bob","StudentAge":20}); { "acknowledged" : true, "insertedId" : ObjectId("5e1758c6cfb11e5c34d898cf") }
Display all documents from a collection with the help of find() method −
> db.demo34.find();
This will produce the following output −
{ "_id" : ObjectId("5e1758b4cfb11e5c34d898cd"), "StudentFirstName" : "Chris", "StudentAge" : 24 } { "_id" : ObjectId("5e1758bdcfb11e5c34d898ce"), "StudentFirstName" : "David", "StudentAge" : 23 } { "_id" : ObjectId("5e1758c6cfb11e5c34d898cf"), "StudentFirstName" : "Bob", "StudentAge" : 20 }
Following is the query to update MongoDB row −
> db.demo34.update({_id:ObjectId("5e1758bdcfb11e5c34d898ce")}, ... {$set:{"StudentFirstName" : "Mike","StudentAge" : 19}}); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Display all documents from a collection with the help of find() method −
> db.demo34.find();
This will produce the following output −
{ "_id" : ObjectId("5e1758b4cfb11e5c34d898cd"), "StudentFirstName" : "Chris", "StudentAge" : 24 } { "_id" : ObjectId("5e1758bdcfb11e5c34d898ce"), "StudentFirstName" : "Mike", "StudentAge" : 19 } { "_id" : ObjectId("5e1758c6cfb11e5c34d898cf"), "StudentFirstName" : "Bob", "StudentAge" : 20 }
- Related Questions & Answers
- MongoDB query to update only certain fields?
- MongoDB query to convert from ObjectId to String
- MongoDB query to update tag
- Find a document with ObjectID in MongoDB?
- How to run MongoDB query to update only a specific field value?
- Update the last row with search criteria in MongoDB?
- MongoDB query to update the nested document?
- Get only the FALSE value with MongoDB query
- MongoDB query to update array with another field?
- Update only a single document in MongoDB
- MongoDB query to update selected fields
- MongoDB query to update nested document
- Update only specific fields in MongoDB?
- How to generate ObjectID in MongoDB?
- Convert string to objectid in MongoDB?
Advertisements