使用猫鼬按嵌套文档属性查找 [英] Use mongoose to find by a nested documents properties
问题描述
我目前有一个文档集合,每个文档都包含指向该集合中其他文档的数组.我需要查询此集合的文档,其中嵌套在数组中的文档包含某个属性.我希望这可以更清楚地解释我的要求:
I currently have a collection of documents that each contain arrays pointing at other documents within that collection. I need to query this collection for documents where the ones nested in arrays contain a certain property. I hope this will explain my request more clearly:
if doc.list1[0].prop = 'foo' or doc.list2[0].prop = 'foo' then select doc
我曾经尝试过使用.find(),但无济于事.
I have tried using .find() like this but to no avail.
{
'doc.list1': 'foo',
$or: [
{ 'doc.list2': 'foo' }
]
}
我在正确的轨道上吗?因为我不认为我是.这是我从文档中可以看到的最好的内容.
Am I on the right track? Because I don't think I am. This is the best I can gleam from the documentation.
修改
这是我实际的查询初始化,使用与Thomas的建议相同的布局.
Here is my actual query initialisation using the same layout as Thomas's suggestion.
var query = this.Word.find({
$or: [
{ 'before.0.cleanWord': topic },
{ 'after.0.cleanWord': topic },
{ 'cleanWord': topic }
]
});
推荐答案
{
$or: [
{ 'doc.list1.0.prop': 'foo' }
{ 'doc.list2.0.prop': 'foo' }
]
}
http://www.mongodb.org/display/DOCS /Advanced + Queries#AdvancedQueries-%24or
这篇关于使用猫鼬按嵌套文档属性查找的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!