Aggregate documents where objects in array matches multiple conditions

﹥>﹥吖頭↗ 提交于 2019-12-20 04:19:25

问题


I have a collection with documents similar to such:

{
    "_id": ObjectId("xxxxx"),
    "item": [
        { "property": ["attr1", "+1"] },
        { "property": ["attr2", "-1"] }
    ]
}

{
    "_id": ObjectId("xxxxy"),
    "item": [
        { "property": ["attr1", "-1"] },
        { "property": ["attr2", "0"] }
    ]
}

{
    "_id": ObjectId("xxxxz"),
    "item": [
        { "property": ["attr1", "0"] },
        { "property": ["attr2", "+1"] }
    ]
}

Preferably using an aggregation pipeline, is there any way to match the document if and only if any one of the properties match more than one condition?

For example, I want a query where one object in the array matches both of these conditions:

("item.property": "attr1") AND ("item.property": /^\+/)

That is, a single property where it contains "attr1" and an element that starts with "+".

However, using my current query that looks like this:

collection.aggregate(
    { $match:
        { $and: 
            [
                { "item.property": "attr1" },
                { "item.property": /^\+/ }
            ]
        }
    }

This would match both the first and last document because both contain a property with "attr1" and an element that stats with "+". However, I do not want this query to match the last document, since the element that starts with "+" does not belong to the same object in the array.

Is there any way to achieve this behavior using the aggregation framework?


回答1:


You can use the below query with $elemMatch to match the array's both values.

Something like

db.collection_name.aggregate({
  "$match": {
    "item": {
      "$elemMatch": {
        "property.0": "attr1",
        "property.1": /^\+/
      }
    }
  }
});

Also, you can use $all operator if you don't want to reference array index.

db.collection_name.aggregate({
  "$match": {
    "item": {
      "$elemMatch": {
        "property": {
          "$all": [
            "attr1",
            /^\+/
          ]
        }
      }
    }
  }
});


来源:https://stackoverflow.com/questions/47825790/aggregate-documents-where-objects-in-array-matches-multiple-conditions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!