【问题标题】:get results from two OR queries with AND in Mongoose在 Mongoose 中使用 AND 从两个 OR 查询中获取结果
【发布时间】: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) ) ) 吗?
  • 不清楚您期望的结果。您希望此处提供的两个文档都与查询匹配吗?如果您展示了您希望匹配条件和不匹配条件的文档,则会更清楚,因为显然您编写查询的方式不起作用。

标签: node.js mongodb mongoose


【解决方案1】:

你可以用like代替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
});

【讨论】:

  • 如果我的 or 条件和查询是动态的,那么我如何获得相同的结果
【解决方案2】:

ANDOR 操作的组合我也遇到了麻烦。这是我研究的一个更简单的例子。这段代码 sn-p 没有给我想要的结果,换句话说,ANDOR 操作的正确组合。

 awTextsDocuments = await AWTextModel
        .find({
          $and: [
            { name: { $regex: reqObj.q, $options: 'i' } },
            { $or: [ recordOwner: req.user._id,  'company-wide-visibility': { $in: 
                     true } ] },

          ],
        })
        .skip(reqObj.offset)
        .limit(reqObj.limit)
        .sort([mongoSortQuery]);

但是一个小小的改变给了我想要的结果:

 awTextsDocuments = await AWTextModel
        .find({
          $and: [
            { name: { $regex: reqObj.q, $options: 'i' } },
            { $or: [{ recordOwner: req.user._id }, { 'company-wide-visibility': { $in: 
                      true } }] },

          ],
        })
        .skip(reqObj.offset)
        .limit(reqObj.limit)
        .sort([mongoSortQuery]);

注意 recordOwner'company-wide-visibility' 前后的括号。这有所作为,并给了我理想的结果。

【讨论】:

    猜你喜欢
    • 2012-10-27
    • 1970-01-01
    • 2021-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-19
    相关资源
    最近更新 更多