【发布时间】:2016-08-08 13:58:30
【问题描述】:
我在猫鼬中将两个 OR 查询与 AND 组合,但没有得到想要的结果
{
_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' } ]
请推荐
【问题讨论】:
-
你想要这样 (A && ( (B || C) && (D || E) ) ) 吗?
-
不清楚您期望的结果。您希望此处提供的两个文档都与查询匹配吗?如果您展示了您希望匹配条件和不匹配条件的文档,则会更清楚,因为显然您编写查询的方式不起作用。