Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.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 有了Mongoid,我可以吗;更新“全部”;一次将多个条目的值推送到数组字段上?_Ruby_Arrays_Mongodb_Mongoid_Update All - Fatal编程技术网

Ruby 有了Mongoid,我可以吗;更新“全部”;一次将多个条目的值推送到数组字段上?

Ruby 有了Mongoid,我可以吗;更新“全部”;一次将多个条目的值推送到数组字段上?,ruby,arrays,mongodb,mongoid,update-all,Ruby,Arrays,Mongodb,Mongoid,Update All,使用Mongoid,是否可以使用“update_all”将一个值推送到数组字段中,用于匹配特定条件的所有条目 例如: class Foo field :username field :bar, :type => Array def update_all_bars array_of_names = ['foo','bar','baz'] Foo.any_in(username: foo).each do |f| f.push(:bar,'my_new

使用Mongoid,是否可以使用“update_all”将一个值推送到数组字段中,用于匹配特定条件的所有条目

例如:

class Foo
  field :username
  field :bar, :type => Array

  def update_all_bars
    array_of_names = ['foo','bar','baz']
    Foo.any_in(username: foo).each do |f|
      f.push(:bar,'my_new_val')
    end
  end
end
我想知道是否有一种方法可以使用“update_all”(或类似的东西)一次更新所有用户(将值“my_new_val”推到每个匹配条目的“foo”字段中),而不是循环一次更新一个用户。我已经尝试了我能想到的一切,但到目前为止没有运气


谢谢

您需要从Mongo DB驱动程序调用它。你可以做:

Foo.collection.update( 
  Foo.any_in(username:foo).selector, 
  {'$push' => {bar: 'my_new_val'}},
  {:multi => true}
)


如果你想在Mongoid builtin中执行pull_请求或功能请求,你可以这样做。

刚刚在写答案,还应该有第三个参数
multi:true
这是什么
:multi=>true
?@shingara给你:
Foo.collection.update( 
  {'$in' => {username: foo}}, 
  {'$push' => {bar: 'my_new_val'}},
  {:multi => true}
)