ruby-on-rails - 在Mongoid中如何找到字段数组计数大于一定数量的所有记录?

我基本上有一个 User 对象并在其中定义了一个数组 field phone_numbers。我想获取数据库中所有 phone_numbers 计数大于 1 的用户对象。

是否有资源可以了解此类查询?我在网上找不到任何有用的东西,因为大多数结果都是针对更简单的查询。

代码:

class Location
  include Mongoid::Document

  field :name
  field :phone_numbers, type: Array
end

我尝试了以下方法,但没有用:

Location.where("this.phone_numbers.count < 1").count

谢谢!

最佳答案

此查询搜索是否存在 phone_numbers[0] 字段中的任何对象

Location.where(:"phone_numbers.0".exists => true).count

此查询解决了获取大于 1 的电话号码的问题;如果存在 phone_numbers[0] 字段,则表示数组中至少有电话号码

https://stackoverflow.com/questions/12432243/

相关文章:

ruby-on-rails-3 - heroku rails logging 不显示输出?

fonts - python matplotlib mathtext AMSeuler 字体

jsf - Primefaces 命令按钮 : f:attribute does not work

build - IntelliJ编译多模块项目

python - 子列表具有共同元素的列表的合并列表

regex - 在 Dreamweaver 中自动执行多个正则表达式搜索/替换查询

winforms - 如何指定 ListView 的 SelectionMode 属性?

visual-studio-2010 - 没有 Team Foundation Server 的 V

azure - 我的 cspack 命令应该是什么来打包 azure 项目

php - 为嵌套对象扩展 json_encode