我在猫鼬中组合了两个与AND的OR查询,但未获得期望的结果
{
_id :"23432ferf",
"catId" : 21,
"attributes": [
{
"_id": "571237bfc02aad440c216f67",
"attributeValueM": "metal|Frame Material"
},
{
"_id": "571237bfc02aad440c216f66",
"attributeValueM": "green|color"
}
]
},
{
_id :"23432ferh",
"catId" : 21,
"attributes": [
{
"_id": "571237bfc02aad440c216f67",
"attributeValueM": "metal|Frame Material"
},
{
"_id": "571237bfc02aad440c216f66",
"attributeValueM": "blue|color"
}
]
}
现在通过猫鼬查询是
var condition = [
{ $or: [{ 'attributes.attributeValueM' : 'wood|Frame Material' }, { 'attributes.attributeValueM' : 'metal/Frame Material' }] },
{ $or: [{ 'attributes.attributeValueM' : 'blue|color' }, {'attributes.attributeValueM' : 'green|color'}] }
];
Test.find("catID":21).and(condition).exec(function (err, results) {
... // not getting any document
});
如何将两个OR条件与AND条件组合在一起?已经在attributes.attributeValueM上建立索引,并且正在使用下面给出的简单AND条件
[ { 'attributes.attributeValueM': 'green|color' },
{ 'attributes.attributeValueM': 'metal|Frame Material' } ]
请建议
而不是Test.find("catID":21).and(condition)
你可以使用像
Test.find({
$and: [
{ catID:21 },
{ $or: [{ "attributes.attributeValueM" : 'wood|Frame Material' }, { "attributes.attributeValueM" : 'metal/Frame Material' }] },
{ $or: [{ "attributes.attributeValueM" : 'blue|color' }, {"attributes.attributeValueM" : 'green|color'}] }
]
}).exec(function (err, results) {
// rest of code
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句