C# 中 MongoDB 更新操作
我想实现 "update mytable set isdel=1 where id="123456789""
我用的是samus驱动
链接我就不说了,说重点的。
这是查询出得数据。
{ "_id" : ObjectId("4ed4a56912842309d072c2ef"), "id" : "123456789", "userid" : "10000", "tiitle" : "test", "content" :"testcontent", "isdel" : 0 }
public int Delete(string id) { Document doc = new Document(); doc["isdel"] = 1; using (MyMongoDb mdb = new MyMongoDb()) { var collection = mdb.GetCollection<LOGS>(); collection.Update(doc, x=>x.id=id); } return 1; }
结果是更新了,可是把整个一行都给更新了只留了一个isdel字段。
{ "_id" : ObjectId("4ed4a56912842309d072c2ef"), "isdel" : 1 }
public int Delete(string id) { Document doc = new Document(); doc["isdel"] = 1; using (MyMongoDb mdb = new MyMongoDb()) { var collection = mdb.GetCollection<LOGS>(); collection.FindAndModify(doc, new Document { { "id", id } }); } return 1; }
看看有没有别的方法吧,找了很久发现了,FindAndModify方法。
执行一下,看看结果。
{ "_id" : ObjectId("4ed4a56912842309d072c2ef"), "id" : "123456789", "userid" : "10000", "tiitle" : "test", "content" :"testcontent", "isdel" : 1 }
成功,呵呵。
这就是Update和FindAndModify的区别。
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。