Back to: MongoDB Tutorials
Replace Document in MongoDB with Examples
In this article, I am going to discuss Replace Document in MongoDB with Examples. Please read our previous article where we discussed Inserting Documents in MongoDB with Examples.
Replace Document in MongoDB
MongoDB documents are also known as BSON documents that are used to store data. The document is created using field-value pairs or key-value pairs and the value is of any BSON type. Here BSON document means the binary representation of JSON document.
db.collection.replaceOne() is used to replace a single document with another document according to the given filter. It does not modify the existing document instead it replaces the whole document with the new one.
- This function will replace the first document that matched the given filter.
- This method replaces one document at a time.
- If the value of upsert is true and no document matches the given filter, then this function will add the replacement document as a new document in the collection.
- In the capped collection if the replaceOne document changes the size of the document then the operation is failed.
- You are not allowed to use replaceOne() function in the time series collections.
- You are allowed to use this function in multi-document transactions.
Syntax:
db.collection.replaceOne(<filter>, <newdocument>,
{upset: <boolean>,
writeConcern: <document>,
collation: <document>,
hint: <document|string>}
)
Parameter:
This function takes the following parameters:
- Filter: It represents the selection criteria. If the value of this field is an empty document({}), then it will replace the very first document of the collection with the new one.
- upsert: It is an optional parameter. By default, it is false. If its value is true, then replaceOne() either insert a replacement document as a new document in the collection if no document matches the given filter. Or replace the old document with the replacement document that matches the given filter.
- writeConcern: It is an optional parameter. It removes the use of default write concern.
- collation: It is an optional parameter. It is used to specify the collation used for the operation.
- hint: It is an optional parameter. It is a document or a string that is used to specify the index to use to support the filter.
Return Value:
This function will return a document that contains the following fields with values:
- acknowledged: The value of this field is true when the insert operation ran with write concern. Otherwise, it will return false.
- matchedCount: It represents the total number of matched documents.
- modifiedCount: It represents the total number of modified documents.
- upsertedID: It represents the _id for the upserted document.
Sample Database
In the following examples we use the following database:
Example – Replace First Document in MongoDB
Here in the below example, we replace the very first document of the author’s collection with the new document.
db.authors.replaceOne({}, {name: “Sohan”, group:”C”, tutorial:”C++”, articleNumber:90})
Example – Replace a document with a new document that matches the filter
Here in the below example, we replace the first document that matches the filter that is the name of the author is “Punit” with the new document.
db.authors.replaceOne({“name.fName”:”Punit”}, {name: “Pihu”, group:”C”, tutorial:”C#”, articleNumber:90})
Example – Insert a New Document in MongoDB
Here in the below example, we insert a new document in the author’s collection because no document was found with the name “Punit” in the author’s collection also the value of upsert is set to true.
db.authors.replaceOne({“name.fName”:”Punit”}, {name: “Mona”, group:”C”, tutorial:”C”, articleNumber:90},
… {upsert: true})
In the next article, I am going to discuss Deleting Documents in MongoDB with Examples. Here, in this article, I try to explain Replace Documents in MongoDB with Examples. I hope you enjoy this Replace Document in MongoDB with Examples article.