使用 C# 驱动程序部分 mongodb upsert?
蒙戈版本1.8.2。
,
public class Acc
{
public int _id { get; set; }
public int? Foo { get; set; }
public int? Bar{ get; set; }
}
Acc a = new Acc
{
_id = 1,
Foo = 3
};
假设我有一个像我想调用的
myCollection.Save(a),
类
- 如果它不存在,则将其插入(到目前为止很容易)
- ,如果它确实存在,则 Foo 会更新,但是 Bar 仍然保持当前的状态(可能不是 - null...)
如何实现部分更新插入?
非常感谢。
Mongo version 1.8.2.
Assume I have a class like
public class Acc
{
public int _id { get; set; }
public int? Foo { get; set; }
public int? Bar{ get; set; }
}
Acc a = new Acc
{
_id = 1,
Foo = 3
};
I'd like to call
myCollection.Save(a),
such that
- if it doesn't exist, its inserted (easy so far)
- if it does exist, Foo is updated, but, but Bar remains whatever it currently is (perhaps non-null...)
How do I achieve this partial upsert?
Many thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通过两次连续更新就可以很容易地做到这一点:
您必须使用 SafeMode.False 来确保如果集合中存在 a ,则插入不会引发异常。
起初您可能会认为这些操作的顺序很重要,但事实并非如此:如果首先执行 2,无论结果如何,1 都会默默地失败。
但是我不知道如何使用 save 方法直接执行此操作。
It would be quite easy to do it with 2 successive updates :
You have to use the SafeMode.False to ensure that if a exists in the collection, the insert won't raise an exception.
At first you would think the order of these operations is important but it isn't : if 2 is executed first, whatever its result, 1 will silently fail.
However I don't have clue on how to use the save method to do this direclty.