加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby – 使用Mongoid,我可以“update_all”将值一次性地推送到多

发布时间:2020-12-17 03:14:00 所属栏目:百科 来源:网络整理
导读:使用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.pus
使用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

我想知道是否有办法一次更新所有用户(将值’my_new_val’推送到每个匹配条目的“foo”字段)使用“update_all”(或类似的东西)而不是循环通过它们来更新他们一次一个.我已经尝试了所有我能想到的东西,到目前为止还没有运气.

谢谢

解决方法

您需要从Mongo DB Driver调用它.你可以做 :

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

要么

Foo.collection.update( 
  {'$in' => {username: foo}},{:multi => true}
)

您可以在Mongoid内置中执行pull_request或功能请求.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读