【问题标题】:what does where(:genres.in => [ "rock" ] mean in Mongoid and how can I find the reference?where(:genres.in => [ "rock" ] 在 Mongoid 中是什么意思?如何找到参考?
【发布时间】:2017-10-13 07:56:56
【问题描述】:

谁知道where(:genres.in => [ "rock" ]在mongoid中是什么意思?
我在文档中看到了这些代码:

class Band
  include Mongoid::Document
  field :country, type: String
  field :genres, type: Array

  scope :english, ->{ where(country: "England") }
  scope :rock, ->{ where(:genres.in => [ "rock" ]) }
end

https://docs.mongodb.com/mongoid/master/tutorials/mongoid-queries/#named-scopes

似乎这意味着找到流派包含“摇滚”但我不确定的文档,因为我找不到解释in的参考。

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 mongoid


    【解决方案1】:

    这是一个小型 ruby​​ DSL,几乎可以直接映射到 mongodb 查询运算符。也就是说,这条红宝石线:

    Band.where(:genres.in => [ "rock" ])
    

    将转换为这个 mongodb 查询(这里使用 shell 语法)

    db.bands.find({ genres: { $in: ['rock']}})
    

    age.gt 等也是如此。这是底层 mongodb 查询运算符的列表:https://docs.mongodb.com/manual/reference/operator/query/#query-selectors

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-20
      • 2019-06-27
      • 1970-01-01
      • 2010-10-25
      • 2018-03-30
      相关资源
      最近更新 更多