更新实体的最佳方法是什么?

时间:2019-01-04 10:53:05

标签: c# mongodb entity-framework repository-pattern

我正在使用C#和mongoDB创建应用程序。我已经在mongoDB中实现了与数据相关的操作的存储库模式。

但是,我正在寻找更新mongoDB中实体的有效方法。当前,我们正在使用GetByID方法获取文档,然后在内存中进行操作,然后调用save方法更新MongoDB服务器中的文档。

Participant p = await participantRepo.GetByIdAsync(ParticipantId);
p.SetCreateAuditInfo(user.Id);
p.AddRelation(user.Id);
participantRepo.SaveAsync(p);

但是由于我们正在将文档加载到内存中,所以当文档太大或太大时,这看起来不是合适的方式。有没有一种方法可以直接在MongoDB Server中更新文档

1 个答案:

答案 0 :(得分:1)

有很多功能可用于更新现有mongodb文档的某些部分。

文档(http://mongodb.github.io/mongo-csharp-driver/2.7/reference/driver/crud/writing/)中的示例:

ints

(一如既往,还有打字版本可用。)

您将在此处找到可用运算符的列表:https://docs.mongodb.com/manual/reference/operator/update/

当您查看c#中的更新构建器类时,将发现这些运算符。这是“设置”运算符的示例。

internal class ConfiguredService // Grayed out
{
    internal string RemoteSystem { get; } // Grayed out
    internal string LocalSystem { get; } // Grayed out
    internal string Configuration { get; } // Not grayed
    internal string ints { get; } // Not grayed
}