本文介绍了如何根据多个条件查找嵌入的Mongoid文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个包含嵌入式文档的Mongoid文档.我想在所有包含顶级文档的嵌入式文档中搜索所有顶级文档.
I have a Mongoid document with embedded documents in it. I want to search the top-level documents for all of them where there is an embedded document that has multiple criteria.
TopDoc.where('inside.first_name' => 'Bob', 'inside.last_name' => 'Jones')
但是在我看来,这将与Bob Wever和Paul Jones在TopDoc上匹配,这是错误的.
But it seems to me this would match on a TopDoc with Bob Wever and Paul Jones, which is wrong.
推荐答案
您需要使用 $ elemMatch .对于Mongoid,以下几行应该可以解决问题
You need to use $elemMatch. With Mongoid, the following line should do the trick
TopDoc.elem_match(inside: { first_name: 'Bob', last_name: 'Jones' })
等效于:
TopDoc.where(:inside.elem_match => { first_name: 'Bob', last_name: 'Jones'})
这篇关于如何根据多个条件查找嵌入的Mongoid文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!