
- 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
Updating data in MongoDB
To update data in MongoDB, use update(). Let us create a collection with documents −
> db.demo110.insertOne({"Name":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e2eeb949fd5fd66da21447b") } > db.demo110.insertOne({"Name":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5e2eeb9f9fd5fd66da21447c") } > db.demo110.insertOne({"Name":"Bob"}); { "acknowledged" : true, "insertedId" : ObjectId("5e2eeba39fd5fd66da21447d") }
Display all documents from a collection with the help of find() method −
> db.demo110.find();
This will produce the following output −
{ "_id" : ObjectId("5e2eeb949fd5fd66da21447b"), "Name" : "Chris" } { "_id" : ObjectId("5e2eeb9f9fd5fd66da21447c"), "Name" : "David" } { "_id" : ObjectId("5e2eeba39fd5fd66da21447d"), "Name" : "Bob" }
Following is the query to update data in MongoDB −
>db.demo110.update({_id:ObjectId("5e2eeb9f9fd5fd66da21447c")},{$set:{"Name":"Robert"}}); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Display all documents from a collection with the help of find() method −
> db.demo110.find();
This will produce the following output −
{ "_id" : ObjectId("5e2eeb949fd5fd66da21447b"), "Name" : "Chris" } { "_id" : ObjectId("5e2eeb9f9fd5fd66da21447c"), "Name" : "Robert" } { "_id" : ObjectId("5e2eeba39fd5fd66da21447d"), "Name" : "Bob" }
- Related Articles
- Updating sub-object in MongoDB?
- Updating nested document in MongoDB
- Updating Nested Embedded Documents in MongoDB?
- Updating MongoDB collection for _id?
- Updating an array with $push in MongoDB
- Update objects in a MongoDB documents array (nested updating)?
- Conditional upsert (multiple insert) when updating document in MongoDB?
- Fastest way of updating in MongoDB is update() or save()?
- Updating Data source of provider by REST API in SAP
- How to use custom variable while updating a MongoDB document?
- Updating a MongoDB document and adding new keys only in the first document?
- Updating a set of documents from a list of key value pairs in MongoDB
- MongoDB syntax for updating an object inside an array within a document?
- Delete partial data in MongoDB?
- Updating Strings in Python

Advertisements