Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby中的GridFS:如何升级?_Ruby_Mongodb_Upsert_Gridfs - Fatal编程技术网

Ruby中的GridFS:如何升级?

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

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 #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所做的,首先删除,然后放入