如何将这两个单独的聚合查询合并为一个查询?

时间:2020-01-30 16:38:35

标签: node.js mongodb mongodb-query aggregation-framework

我在mongo中发布了两个有关聚合查询的问题。

Question 1获取特定人

的所有杂务

Question 2在特定的序号(天)(日期)

中获取所有人的所有杂务

但是,现在,我想将这两个查询合并为一个查询。仅返回特定日期特定人的琐事

这是我到目前为止所拥有的:

        ChoreChart.aggregate([
            { "$match": { "affiliation": affiliation, "year": weekYear, "weekNumber": weekNumber } },
            { "$addFields": {
              "chart": {
                "$map": {
                  "input": "$chart",
                  "as": "cc",
                  "in": {
                    "_id": "$$cc._id",
                    "ordinal": "$$cc.ordinal",
                    "ordinalString": "$$cc.ordinalString",
                    "chorePerson": {
                      "$filter": {
                        "input": "$$cc.chorePerson",
                        "as": "dd",
                        "cond": 
                            { 
                            "$and": [ 
                                {"$eq": ["$$dd.personID", personID]},
                                {"$eq": ["$$cc.ordinal", ordinal ]}
                            ] }                        
                      }
                    }
                  }
                }
              }
            }}
          ])

这是我得到序数为4的结果,但是,我的输出包括我不感兴趣的序数。

[
  {
    "_id": "5e2d482cd8593e00162d0568",
    "affiliation": "800_800",
    "year": 2020,
    "month": "January",
    "weekNumber": 5,
    "weekStart": "01/26/2020",
    "weekEnd": "02/01/2020",
    "chart": [
      {
        "_id": "5e330310c66e9e4084cda785",
        "ordinal": 0,
        "ordinalString": "Sunday",
        "chorePerson": []
      },
      {
        "_id": "5e330310c66e9e4084cda783",
        "ordinal": 1,
        "ordinalString": "Monday",
        "chorePerson": []
      },
      {
        "_id": "5e330310c66e9e4084cda780",
        "ordinal": 2,
        "ordinalString": "Tuesday",
        "chorePerson": []
      },
      {
        "_id": "5e330310c66e9e4084cda77e",
        "ordinal": 3,
        "ordinalString": "Wednesday",
        "chorePerson": []
      },
      {
        "_id": "5e330310c66e9e4084cda77c",
        "ordinal": 4,
        "ordinalString": "Thursday",
        "chorePerson": [
          {
            "_id": "5e330310c66e9e4084cda77d",
            "person": "Jo",
            "personID": "5e2890268c63351b7c07dc26",
            "phone": "8008008001",
            "chore": "DC 1",
            "choreID": "5e2929cf285338cb8cf375fc"
          },
          {
            "_id": "5e330310c66e9e4084cda77e",
            "person": "Jo",
            "personID": "5e2890268c63351b7c07dc26",
            "phone": "8008008001",
            "chore": "DC 2",
            "choreID": "5e2929cf285338cb8cf375fd"
          }
        ]
      },
      {
        "_id": "5e330310c66e9e4084cda77a",
        "ordinal": 5,
        "ordinalString": "Friday",
        "chorePerson": []
      },
      {
        "_id": "5e330310c66e9e4084cda778",
        "ordinal": 6,
        "ordinalString": "Saturday",
        "chorePerson": []
      }
    ],
    "date": "2020-01-30T16:23:44.713Z",
    "__v": 0
  }
]

是我真正想要的:

[
  {
    "_id": "5e2d482cd8593e00162d0568",
    "affiliation": "800_800",
    "year": 2020,
    "month": "January",
    "weekNumber": 5,
    "weekStart": "01/26/2020",
    "weekEnd": "02/01/2020",
    "chart": [
      {
        "_id": "5e330310c66e9e4084cda77c",
        "ordinal": 4,
        "ordinalString": "Thursday",
        "chorePerson": [
          {
            "_id": "5e330310c66e9e4084cda77d",
            "person": "Jo",
            "personID": "5e2890268c63351b7c07dc26",
            "phone": "8008008001",
            "chore": "DC 1",
            "choreID": "5e2929cf285338cb8cf375fc"
          },
          {
            "_id": "5e330310c66e9e4084cda77e",
            "person": "Jo",
            "personID": "5e2890268c63351b7c07dc26",
            "phone": "8008008001",
            "chore": "DC 2",
            "choreID": "5e2929cf285338cb8cf375fd"
          }
        ]
      }
    ],
    "date": "2020-01-30T16:23:44.713Z",
    "__v": 0
  }
]

1 个答案:

答案 0 :(得分:1)

您可以尝试以下方法:

ChoreChart.aggregate([
    { "$match": { "affiliation": affiliation, "year": weekYear, "weekNumber": weekNumber } }, {
        "$addFields": {
            "chart": {
                "$map": {
                    /** $filter in input to retain only objects in array that match ordinal filter */
                    "input": { $filter: { input: '$chart', as: 'c', cond: { "$eq": ["$$c.ordinal", ordinal] } } },
                    "as": "cp",
                    "in": {
                        /** $mergeObjects to merge chorePerson filtered array field on each 'chart.chorePerson' */
                        $mergeObjects: ['$$cp', { chorePerson: { $filter: { input: '$$cp.chorePerson', as: 'each', cond: { "$eq": ["$$each.personID", personID] } } } }]
                    }
                }
            }
        }
    }
 ])

测试: MongoDB-Playground