[CSHARP-1857] FindOneAndReplace or ReplaceOne is not working sometime Created: 02/Dec/16  Updated: 27/Oct/23  Resolved: 05/Dec/16

Status: Closed
Project: C# Driver
Component/s: Operations
Affects Version/s: 2.2.4, 2.4
Fix Version/s: None

Type: Task Priority: Major - P3
Reporter: Kalpesh Chheda [X] Assignee: Robert Stam
Resolution: Works as Designed Votes: 0
Labels: Bug, question
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Windows Server 2016, Asp.net,



 Description   

public async Task SomeWork()
        {
            Debug.WriteLine("SomeWork for User : " + Context.User.Identity.Name);
            var userProfile = await UserProfileCollection.Find(u => u._id == Context.User.Identity.Name).FirstOrDefaultAsync();
 
            userProfile.SuccessfullConnect = true; // currently false
            var up = await UserProfileCollection.FindOneAndReplaceAsync(u => u._id == userProfile._id, userProfile);
            Debug.WriteLine(ObjectDumper.Dump(userProfile));
            Debug.WriteLine(ObjectDumper.Dump(up));
            Debug.WriteLine("Saved SomeWork for User : " + Context.User.Identity.Name);
        }

Below is the debug output. My document is not getting replaced. Check SuccessfullConnect variable it is still false. I checked on server also document is not replaced. And sometime its working and sometime its not working.

SomeWork for User : +919933221101
{TestSignalR_Server.Models.UserProfile}
  _id: "+919933221101"
  ...
  SuccessfullConnect: True
  ...
 
{TestSignalR_Server.Models.UserProfile}
  _id: "+919933221101"
  ...
  SuccessfullConnect: False
  ...
 
Saved SomeWork for User : +919933221101



 Comments   
Comment by Robert Stam [ 05/Dec/16 ]

FindOneAndReplace by default returns the original version of the replaced document.

The document should be changed in your database, but your local variable up contains a copy of the document as it was before the replacement.

If you want FindOneAndReplace to return the modified document use the options parameter and set the ReturnDocument property to ReturnDocument.After.

Comment by Kalpesh Chheda [X] [ 02/Dec/16 ]

For reading formatted code.
http://stackoverflow.com/questions/40929600/c-sharp-mongodb-findoneandreplace-or-replaceone-is-not-working

Similar issue by other
http://stackoverflow.com/questions/32633345/replaceone-methods-filter-matches-document-does-not-perform-replacement

Generated at Wed Feb 07 21:40:51 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.