Ruby 中的 GridFS:如何更新插入?
GridFS 有 upsert 吗?
例如,如果我想保存具有指定 _id 的图像,并且具有相同 _id 的图像已存在,我希望它覆盖(更新)它。否则,将其插入。
Does GridFS have an upsert?
For example, if i want to save an image with a specified _id, and one with that same _id already exists, i want it to overwrite (update) it. Otherwise, insert it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该规范并不是真正为支持更新插入而设计的,因为从技术上讲您正在修改多个文档,并且肯定会出现棘手的竞争条件。所以我们推荐Matt做的事情,就是先删除再放入。
The spec isn't really designed to support upserts, since you're technically modifying more than one document, and certainly tricky race conditions can arise. So we recommend what Matt has done, which is to delete first and then put.
我查看了 mongo ruby gem 源代码并发现了这一点:
所以,我在代码中执行了此操作:
请参阅此处的工作 sinatra 脚本:
http://github.com/acani/acani-sinatra/ blob/master/acani.rb#L97
I looked at the mongo ruby gem source code and found this:
So, I did this in the code:
See the working sinatra script here:
http://github.com/acani/acani-sinatra/blob/master/acani.rb#L97