
- 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
Update a MongoDB document with Student Id and Name
To update, use UPDATE() and $set. Let us create a collection with documents −
> db.demo427.insertOne({"StudentId":101,"StudentName":"Chris Brown"}); { "acknowledged" : true, "insertedId" : ObjectId("5e75e711bbc41e36cc3cae75") } > db.demo427.insertOne({"StudentId":102,"StudentName":"David Miller"}); { "acknowledged" : true, "insertedId" : ObjectId("5e75e71abbc41e36cc3cae76") } > db.demo427.insertOne({"StudentId":103,"StudentName":"John Smith"}); { "acknowledged" : true, "insertedId" : ObjectId("5e75e725bbc41e36cc3cae77") } > db.demo427.insertOne({"StudentId":104,"StudentName":"Carol Taylor"}); { "acknowledged" : true, "insertedId" : ObjectId("5e75e733bbc41e36cc3cae78") }
Display all documents from a collection with the help of find() method −
> db.demo427.find();
This will produce the following output −
{ "_id" : ObjectId("5e75e711bbc41e36cc3cae75"), "StudentId" : 101, "StudentName" : "Chris Brown" } { "_id" : ObjectId("5e75e71abbc41e36cc3cae76"), "StudentId" : 102, "StudentName" : "David Miller" } { "_id" : ObjectId("5e75e725bbc41e36cc3cae77"), "StudentId" : 103, "StudentName" : "John Smith" } { "_id" : ObjectId("5e75e733bbc41e36cc3cae78"), "StudentId" : 104, "StudentName" : "Carol Taylor" }
Following is the query to update MongoDB document −
> db.demo427.update({"StudentId":102},{$set:{"StudentName":"John Doe"}}); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Display all documents from a collection with the help of find() method −
> db.demo427.find();
This will produce the following output −
{ "_id" : ObjectId("5e75e711bbc41e36cc3cae75"), "StudentId" : 101, "StudentName" : "Chris Brown" } { "_id" : ObjectId("5e75e71abbc41e36cc3cae76"), "StudentId" : 102, "StudentName" : "John Doe" } { "_id" : ObjectId("5e75e725bbc41e36cc3cae77"), "StudentId" : 103, "StudentName" : "John Smith" } { "_id" : ObjectId("5e75e733bbc41e36cc3cae78"), "StudentId" : 104, "StudentName" : "Carol Taylor" }
- Related Articles
- How to update document with marks value in MongoDB for student David
- Display MongoDB with document and subdocument example and update
- Update a specific MongoDB document in array with $set and positional $ operator?
- Update two separate arrays in a document with one update call in MongoDB?
- How to create a DB2 table TAB1 with 4 columns, Student ID, Enrollment ID, Name and Age?
- MongoDB findOneAndUpdate() to update a single document
- Update only a single document in MongoDB
- How do I get email-id from a MongoDB document and display with print()
- MongoDB query to update nested document
- Update only a specific value in a MongoDB document
- Update a single list item of a MongoDB document?
- How do you update a MongoDB document while replacing the entire document?
- MongoDB query to update the nested document?
- How to update the _id of a MongoDB Document?
- MongoDB query to update a specific document from a collection

Advertisements