I am using FindAndModify
to modify a document.
The document is of type User
and the element to modify is called web
:
var users = _db.GetCollection<User>(UserCollectionName);
var userQuery = Query.EQ("user", "testuser");
var findAndModifyResult = users.FindAndModify(
new FindAndModifyArgs()
{
Query = userQuery,
Update = Update.Set("web", "testweb")
});
// user.web is unchanged in the result
var user = findAndModifyResult.GetModifiedDocumentAs<User>();
// user.web is changed in the result
user = users.FindOne(userQuery);
GetModifiedDocumentAs()
doesn't return the changed instance, user.web
still has the same value it had before the update.
When I query the user
with FindOne()
I see the changed value.
Is there something I need to take care of, so that FindAndModify()
returns the modified document?
To expand on Will Shavers' answer, this is the correct method with the c# driver:
collection.FindAndModify(
new FindAndModifyArgs()
{
Query = query,
Update = updateOperation,
// this needs to be set
VersionReturned = FindAndModifyDocumentVersion.Modified
});
You need to query with : {safe: true, 'new' : true}
I'm not sure how to send these options in the c# driver.
http://docs.mongodb.org/ecosystem/tutorial/use-csharp-driver/#findandmodify-method
Looks like the final argument is the "new" option.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With