使用c#驱动程序进行部分mongodb升级?
Mongo版本1.8.2 假设我有一门课使用c#驱动程序进行部分mongodb升级?,mongodb,mongodb-.net-driver,Mongodb,Mongodb .net Driver,Mongo版本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), 以致 如果它不存在,则插入它(到目前为止很容易) 如
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…)
非常感谢。通过连续两次更新,您可以轻松完成此任务:
myCollection.Insert(a,SafeMode.False);
myCollection.Update(Query.EQ("_id",a._id), Update.Set("Foo",a.Foo))
您必须使用SafeMode.False以确保如果集合中存在,则插入不会引发异常
起初,您可能认为这些操作的顺序很重要,但事实并非如此:如果先执行2,无论结果如何,1都会自动失败
但是,我不知道如何使用save方法直接执行此操作