Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如何将这两个单独的聚合查询合并到一个查询中?_Node.js_Mongodb_Mongodb Query_Aggregation Framework - Fatal编程技术网

Node.js 如何将这两个单独的聚合查询合并到一个查询中?

Node.js 如何将这两个单独的聚合查询合并到一个查询中?,node.js,mongodb,mongodb-query,aggregation-framework,Node.js,Mongodb,Mongodb Query,Aggregation Framework,我在mongo发布了两个关于聚合查询的问题 获取特定人员的所有家务 获取特定序号(天)上所有人的所有家务 但是,现在我想将这两个查询合并到一个查询中。只返回特定日期特定人员的家务 以下是我迄今为止所做的: ChoreChart.aggregate([ { "$match": { "affiliation": affiliation, "year": weekYear, "weekNumber": weekNumber } }, {

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

获取特定人员的所有家务

获取特定序号(天)上所有人的所有家务

但是,现在我想将这两个查询合并到一个查询中。只返回特定日期特定人员的家务

以下是我迄今为止所做的:

        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
  }
]
您可以尝试以下方法:

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] } } } }]
                    }
                }
            }
        }
    }
 ])

测试:

你所说的特定的第几天是什么意思?你说的“序数”是指“星期日”等吗。。。在这种情况下是一周中的哪一天,例如0=星期天,1=星期一,2=星期三。。。等“有序字符串”是为人类可读性而存在的非常感谢你。。。我就是不能把我的头放在Mongo的东西上。。。我想我太像一个线性思考者了,而不是一个“固定思考者”——再次感谢!