gpt4 book ai didi

C# Mongodb FindOneAndReplace 或 ReplaceOne 不起作用

转载 作者:IT老高 更新时间:2023-10-28 12:31:25 38 4
gpt4 key购买 nike

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);
}

下面是调试输出。我的文件没有被替换。检查成功连接变量它仍然是错误的。我在服务器上检查过文件也没有被替换。有时它工作,有时它不工作。

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

最佳答案

方法 FindOneAndReplaceAsync 应该可以正常工作。
但默认情况下,此方法返回原始文档。您可以将 ReturnDocument 更改为 ReturnDocument.After 以查看更改后的文档。

var options = new FindOneAndReplaceOptions<Profile>
{
ReturnDocument = ReturnDocument.After
};
var up = await collection.FindOneAndReplaceAsync<Profile>(u => u._id == userProfile._id, userProfile, options);

关于C# Mongodb FindOneAndReplace 或 ReplaceOne 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40929600/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com