Mongodb/Mongoid-{:multi =>真}的意思 [英] Mongodb/Mongoid - what does {:multi => true} mean

查看:113
本文介绍了Mongodb/Mongoid-{:multi =>真}的意思的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于此问题:

我想问:

  1. {:multi => true}在这里的目的是什么?
  2. 现在通过蒙古语update_all时是否可以将值推入数组?因为问题是在2010年.
  1. What's the purpose of {:multi => true} here?
  2. Is it possible to push a value into an array when update_all via mongoid now? because the question is in 2010.

谢谢.

推荐答案

MongoDB更新方法指出以下内容:

multi -指示是否应更新所有符合条件的文档,而不仅仅是一个.可以与下面的$运算符一起使用.

multi - indicates if all documents matching criteria should be updated rather than just one. Can be useful with the $ operators below.

因此,基本上multi参数是使链接到的问题中的update_all行为有效的原因.

So basically the multi parameter is what enables the update_all behaviour in the question you linked to.

回答第二个问题:是的-Mongoid现在已内置此功能.文档参考位于此处.但是您可以这样使用它:

In answer to your second question: yes - Mongoid has this feature built in now. The documentation reference is here. But you can use it like this:

User.where(:gender => "Male").update_all(:title => "Mr")

更新

在要将值推入数组字段的情况下,仍然需要直接使用MongoDB库,因为Mongoid update_all方法仅支持$set数据库更新方法(可以是用于更新整个数组,但不将值压入该数组).

In the case where you want to push a value onto an array field, you'll still need to use the MongoDB library directly, since the Mongoid update_all method only supports the $set database update method (which can be used to update an entire array, but not push values onto it).

您所链接的问题的答案中的示例将起作用,我已将其复制到偶然发现此问题的人员下方(感谢 shingara !):

The example in the answer to the question you linked to would work, I have copied it below those who stumble across this question (thanks shingara!):

User.collection.update( 
  {'$in' => {:gender => 'Male'}}, 
  {'$push' => {:titles => 'Mr'}},
  {:multi => true}
)

这篇关于Mongodb/Mongoid-{:multi =>真}的意思的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆