使用 mongo-ruby-driver 进行文档更新?

发布于 2024-10-13 18:23:24 字数 437 浏览 6 评论 0原文

假设如下:

irb> x
irb> => {"_id"=> 123456, "welcome"=>"Hi!", "welcome2" => "Enjoy your stay!"}
irb> coll.class
irb> => Mongo::Collection

如何使用原始的 mongo-ruby-driver 通过重写方法和原子更新方法来更新 x 对应的文档? (参见http://api.mongodb.org/ruby/current/file .TUTORIAL.html#Updating_a_Document)

Assuming the following:

irb> x
irb> => {"_id"=> 123456, "welcome"=>"Hi!", "welcome2" => "Enjoy your stay!"}
irb> coll.class
irb> => Mongo::Collection

How can I use the raw mongo-ruby-driver to update the document corresponding to x by using both the rewriting method and the atomic update method? (See http://api.mongodb.org/ruby/current/file.TUTORIAL.html#Updating_a_Document)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风柔一江水 2024-10-20 18:23:24

给定您的示例输出,如果您想使用重写方法,它将像这样:

coll.update({"_id" => x["_id"]}, x)

或者如果您想原子地更改一个值,它将像这样:

coll.update({"_id" => x["_id"]}, {"$set" => {"welcome" => "Hello There"}})

given your example output, if you want to use the rewriting method it would be like this:

coll.update({"_id" => x["_id"]}, x)

or if you want to atomically change a value, it would be like this:

coll.update({"_id" => x["_id"]}, {"$set" => {"welcome" => "Hello There"}})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文