Ruby中的GridFS:如何升级?
GridFS有一个upsert吗Ruby中的GridFS:如何升级?,ruby,mongodb,upsert,gridfs,Ruby,Mongodb,Upsert,Gridfs,GridFS有一个upsert吗 例如,如果我想保存一个具有指定_id的图像,并且已经存在一个具有相同_id的图像,我希望它覆盖(更新)它。否则,请插入它。我查看了mongo ruby gem源代码,发现: # Store a file in the file store. This method is designed only for writing new files; # if you need to update a given file, first delete it using
例如,如果我想保存一个具有指定_id的图像,并且已经存在一个具有相同_id的图像,我希望它覆盖(更新)它。否则,请插入它。我查看了mongo ruby gem源代码,发现:
# Store a file in the file store. This method is designed only for writing new files;
# if you need to update a given file, first delete it using #Grid#delete.
# ...
def put(data, opts={})
因此,我在代码中这样做:
grid.delete(id) # if exists
grid.put(tmp_file.read, :_id => id, :content_type => file_type)
请参见此处的工作sinatra脚本:
由于您在技术上修改了多个文档,因此该规范的设计并非真正支持upserts,而且肯定会出现棘手的竞争条件。因此,我们推荐Matt所做的,首先删除,然后放入