Replace
Last updated
Last updated
You can replace a single document entirely by using the ReplaceOne
method on an IMongoCollection<T>
.
The sample replaces the first document with a new one.
The identifier field _id on the new document must fulfill one of the following conditions:
If specified, must be equal to the current document's value
Not specified at all. MongoDB will create a new one automatically
In case you do set a value for the identifier _id field and it's different than the current one, you will get the exception:After applying the update, the (immutable) field '_id' was found to have been altered to _id: <new-id>
One common scenario where you need to replace documents is when you want to move fields inside the documents. Consider that you have the following document.
The following sample moves friends and blocked top level fields in a new embedded document field named relationships.
If the filter in the ReplaceOne
operation fails to match a document then nothing happens in the database. You can change this behavior by passing a replace options argument to the replace operation and setting upsert = true
.
The sample tries to replace a user document that has company name "Microsoft Corp". If it finds a match then it will replace it with the microsoftCeo document but if it doesn't, it will insert it.
IMongoCollection<T>
contains a FindOneAndReplaceOne
method that behaves exactly the same as the ReplaceOne
except that the returned result is of type T
instead of a ReplaceOneResult
, in other words it returns the updated or upserted document itself. This can be quite convenient when you want to keep working with the new document after replacing it.
The sample replaces the first document with a new one and gets back the entire document.
When using the FindOneAndReplace
method you have two options for the returned result:
Return the updated document - you need to set ReturnDocument = ReturnDocument.After
in the FindOneAndReplaceOptions
Return the document before being updated - you need to set ReturnDocument = ReturnDocument.Before
in the FindOneAndREplaceOptions
or leave it as is since it's the default value
Since you set upsert = true
a new document inserted with _id equal to the upsertedId