入门客AI创业平台(我带你入门,你带我飞行)
博文笔记

C# 中 MongoDB 更新操作

创建时间:2011-11-29 投稿人: 浏览次数:6041

我想实现 "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的区别。

 

声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。
  • 上一篇:没有了
  • 下一篇:没有了
未上传头像