【问题标题】:How can I find embedded Mongoid documents based on multiple criteria?如何根据多个条件查找嵌入式 Mongoid 文档?
【发布时间】:2013-02-08 16:18:22
【问题描述】:

我有一个包含嵌入文档的 Mongoid 文档。我想在顶级文档中搜索所有包含多个条件的嵌入式文档。

TopDoc.where('inside.first_name' => 'Bob', 'inside.last_name' => 'Jones')

但在我看来,这与 Bob Wever 和 Paul Jones 的 TopDoc 匹配,这是错误的。

【问题讨论】:

    标签: ruby-on-rails mongodb mongoid


    【解决方案1】:

    您需要使用$elemMatch。使用 Mongoid,以下行应该可以解决问题

    TopDoc.elem_match(inside: { first_name: 'Bob', last_name: 'Jones' })
    

    相当于:

    TopDoc.where(:inside.elem_match => { first_name: 'Bob', last_name: 'Jones'})
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-21
      • 2023-03-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多