
- 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
How do you update a MongoDB document while replacing the entire document?
Let us first create a collection with a document −
>db.replacingEntireDocumentDemo.insertOne({"StudentFirstName":"John","StudentLastName":"Smith","StudentCountryName":"US"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd3119bb64f4b851c3a13e8") }
Following is the query to display document from a collection with the help of find() method −
> db.replacingEntireDocumentDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cd3119bb64f4b851c3a13e8"), "StudentFirstName" : "John", "StudentLastName" : "Smith", "StudentCountryName" : "US" }
Following is the query to update a MongoDB document while replacing the entire document −
>db.replacingEntireDocumentDemo.update({"StudentFirstName":"John","StudentLastName":"Smith","StudentCountryName":"US"}, {"StudentFirstName":"David","StudentLastName":"Miller","StudentCountryName":"AUS"}); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Let us display all the records from the collection once again −
> db.replacingEntireDocumentDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cd3119bb64f4b851c3a13e8"), "StudentFirstName" : "David", "StudentLastName" : "Miller", "StudentCountryName" : "AUS" }
- Related Articles
- In MongoDB how do you use $set to update a nested value/embedded document?
- How to update the _id of a MongoDB Document?
- MongoDB findOneAndUpdate() to update a single document
- Update only a single document in MongoDB
- MongoDB query to update the nested document?
- MongoDB query to update nested document
- How to update a MongoDB document without overwriting the existing one?
- Update only a specific value in a MongoDB document
- Update a single list item of a MongoDB document?
- How to use custom variable while updating a MongoDB document?
- Update a MongoDB document with Student Id and Name
- Update array in MongoDB document by variable index?
- MongoDB query to update a specific document from a collection
- Update only a single MongoDB document without deleting any date
- How to prevent MongoDB from returning the object ID while finding a document?

Advertisements