在 Mongoose 中使用 AND 从两个 OR 查询中获取结果

PRE*_*DAV 3 mongoose mongodb node.js

我正在将两个 OR 查询与 mongoose 中的 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"
        }
  ]
}
Run Code Online (Sandbox Code Playgroud)

现在通过猫鼬查询是

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
      });
Run Code Online (Sandbox Code Playgroud)

我如何将两个 OR 条件与 AND 条件结合起来? 已经在 attributes.attributeValueM 上建立索引,它正在使用下面给出的简单 AND 条件

[ { 'attributes.attributeValueM': 'green|color' },
  { 'attributes.attributeValueM': 'metal|Frame Material' } ]
Run Code Online (Sandbox Code Playgroud)

请建议

Sha*_*Roy 6

而不是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
});
Run Code Online (Sandbox Code Playgroud)