
- MongoDB Tutorial
- MongoDB - Home
- MongoDB - Overview
- MongoDB - Advantages
- MongoDB - Environment
- MongoDB - Data Modeling
- MongoDB - Create Database
- MongoDB - Drop Database
- MongoDB - Create Collection
- MongoDB - Drop Collection
- MongoDB - Data Types
- MongoDB - Insert Document
- MongoDB - Query Document
- MongoDB - Update Document
- MongoDB - Delete Document
- MongoDB - Projection
- MongoDB - Limiting Records
- MongoDB - Sorting Records
- MongoDB - Indexing
- MongoDB - Aggregation
- MongoDB - Replication
- MongoDB - Sharding
- MongoDB - Create Backup
- MongoDB - Deployment
- MongoDB - Java
- MongoDB - PHP
- Advanced MongoDB
- MongoDB - Relationships
- MongoDB - Database References
- MongoDB - Covered Queries
- MongoDB - Analyzing Queries
- MongoDB - Atomic Operations
- MongoDB - Advanced Indexing
- MongoDB - Indexing Limitations
- MongoDB - ObjectId
- MongoDB - Map Reduce
- MongoDB - Text Search
- MongoDB - Regular Expression
- Working with Rockmongo
- MongoDB - GridFS
- MongoDB - Capped Collections
- Auto-Increment Sequence
- MongoDB Useful Resources
- MongoDB - Questions and Answers
- MongoDB - Quick Guide
- MongoDB - Useful Resources
- MongoDB - Discussion
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 Articles
- MongoDB query to update only certain fields?
- MongoDB query to convert from ObjectId to String
- How to run MongoDB query to update only a specific field value?
- MongoDB query to update tag
- MongoDB query to update array with another field?
- Update the last row with search criteria in MongoDB?
- Find a document with ObjectID in MongoDB?
- MongoDB query to update the nested document?
- MongoDB query to update selected fields
- MongoDB query to update nested document
- Get only the FALSE value with MongoDB query
- MongoDB query to update only a single item from voting (up and down) records?
- How to update many documents with one query in MongoDB?
- Update only a single document in MongoDB
- Resolve ‘multi update only works with $ operators’ in MongoDb?

Advertisements