将数组内容过滤到包含$ concatArrays的查询

时间:2017-08-22 05:23:17

标签: mongodb mongodb-query aggregation-framework

鉴于此功能,我有一个我正在查询的数据集。数据如下所示:

db.activity.insert(
    {
        "_id" : ObjectId("5908e64e3b03ca372dc945d5"),
        "startDate" : ISODate("2017-05-06T00:00:00Z"),
        "details" : [
            {
                "code" : "2",
                "_id" : ObjectId("5908ebf96ae5003a4471c9b2"),
                "walkDistance" : "03",
                "jogDistance" : "01",
                "runDistance" : "08",
                "sprintDistance" : "01"
            }
        ]
    }
)

db.activity.insert(
    {
        "_id" : ObjectId("58f79163bebac50d5b2ae760"),
        "startDate" : ISODate("2017-05-07T00:00:00Z"),
        "details" : [
            {
                "code" : "2",
                "_id" : ObjectId("58f7948fbebac50d5b2ae7f2"),
                "walkDistance" : "01",
                "jogDistance" : "02",
                "runDistance" : "09",
                "sprintDistance" : ""
            }
        ]
    }
)

使用此功能,感谢Neil Lunn,我能够得到我想要的输出:

db.activity.aggregate([
  { "$project": {
    "_id": 0,
    "unique": {
      "$filter": {
        "input": {
          "$setDifference": [
            { "$concatArrays": [ 
              "$details.walkDistance",
              "$details.jogDistance",
              "$details.runDistance",
              "$details.sprintDistance"
            ]},
            []
          ]
        },
        "cond": { "$ne": [ "$$this", "" ] }
      }
    }
  }},
  { "$unwind": "$unique" },
  { "$group": {
    "_id": null,
    "uniqueArray": { "$addToSet": "$unique" }  
  }}
])

但是,我不能在开头添加匹配语句。

db.activity.aggregate([
  {$match: {"startDate" : ISODate("2017-05-06T00:00:00Z"), "details.code": "2" },
  {$unwind: '$details'},
  {$match: {"startDate" : ISODate("2017-05-06T00:00:00Z"), "details.code": "2" },
  { "$project": {
    "_id": 0,
    "unique": {
      "$filter": {
        "input": {
          "$setDifference": [
            { "$concatArrays": [ 
              "$details.walkDistance",
              "$details.jogDistance",
              "$details.runDistance",
              "$details.sprintDistance"
            ]},
            []
          ]
        },
        "cond": { "$ne": [ "$$this", "" ] }
      }
    }
  }},
  { "$unwind": "$unique" },
  { "$group": {
    "_id": null,
    "uniqueArray": { "$addToSet": "$unique" }  
  }}
])

因为它给出了错误消息:

> $concatArrays only supports arrays, not string

如何修改此查询以便添加$match语句?

1 个答案:

答案 0 :(得分:1)

不要$unwind您要向$concatArrays投放的阵列。而是应用$filter仅提取匹配值。如上所述,我们只需使用$setUnion即可获得'唯一连接'代替:

db.activity.aggregate([
  { "$match": { "startDate" : ISODate("2017-05-06T00:00:00Z"), "details.code": "2" } },
  { "$project": {
    "_id": 0,
    "unique": {
      "$let": {
        "vars": {
          "filtered": {
            "$filter": {
              "input": "$details",
              "cond": { "$eq": [ "$$this.code", "2" ] }
            }  
          }
        },
        "in": {
          "$setDifference": [
            { "$setUnion": [ 
              "$$filtered.walkDistance",
              "$$filtered.jogDistance",
              "$$filtered.runDistance",
              "$$filtered.sprintDistance"
            ]},
            [""]
          ]
        } 
      }
    }
  }},
  { "$unwind": "$unique" },
  { "$group": {
    "_id": null,
    "uniqueArray": { "$addToSet": "$unique" }  
  }}
])

使用$let可以使语法更清晰,因为您不需要指定多个$map$filter语句" inline"作为$setUnion

的来源