MongoDB$放松和性能

MongoDB$放松和性能,mongodb,Mongodb,下面是我的数据的一个小例子: [ { "callId": "17dac51e-125e-499e-9064-f20bd3b1a9d8", "caller": { "firstName": "Test", "lastName": "Testing", "phoneNumber": "1231231234" }, "inquiries": [ { "inquiryId": "b0d14381-ce75

下面是我的数据的一个小例子:

[
  {
    "callId": "17dac51e-125e-499e-9064-f20bd3b1a9d8",
    "caller": {
      "firstName": "Test",
      "lastName": "Testing",
      "phoneNumber": "1231231234"
    },
    "inquiries": [
      {
        "inquiryId": "b0d14381-ce75-49aa-a66a-c36ae20b72a8",
        "routeHistory": [
          {
            "assignedUserId": "cfa0ffe9-c77d-4eec-87d7-4430f7772e81",
            "routeDate": "2020-01-01T06:00:00.000Z",
            "status": "routed"
          },
          {
            "assignedUserId": "cfa0ffe9-c77d-4eec-87d7-4430f7772e81",
            "routeDate": "2020-01-03T06:00:00.000Z",
            "status": "routed"
          }
        ]
      },
      {
        "inquiryId": "9d743be9-7613-46d7-8f9b-a04b4b899b56",
        "routeHistory": [
          {
            "assignedUserId": "cfa0ffe9-c77d-4eec-87d7-4430f7772e81",
            "routeDate": "2020-01-01T06:00:00.000Z",
            "status": "ended"
          },
          {
            "assignedUserId": "cfa0ffe9-c77d-4eec-87d7-4430f7772e81",
            "routeDate": "2020-01-03T06:00:00.000Z",
            "status": "ended"
          }
        ]
      }
    ]
  }
]
我正在针对更多文档运行以下聚合:

db.collection.aggregate([
    {
        $unwind: "$inquiries"
    },
    {
        $match: {
            "inquiries.routeHistory.status": "ended"
        }
    },
    {
        $addFields: {
            "inquiries.routeHistory": {
                $filter: {
                    input: "$inquiries.routeHistory",
                    cond: {
                        $eq: [ { $max: "$inquiries.routeHistory.routeDate" }, "$$this.routeDate" ]
                    }
                }
            }
        }
    },
    {
        $group: {
            _id: "$_id",
            callId: { $first: "$callId" },
            caller: { $first: "$caller" },
            inquiries: { $push: "$inquiries" }
        }
    }
])
虽然这确实返回了预期的结果,但它不能扩展。当我在一个更大的数据集上运行它时,我会超时。我在查询的字段上有索引。有没有办法优化我的查询以获得更好的性能


重要注意事项:我仅限于使用

支持的运算符。它不可缩放,因为如果
$match
(以及其他,请查看)发生在管道的开头,则可以应用索引

确保您有以下索引:
{'inquiries.routeHistory.status':1}

db.collection.aggregate([
  {
    $match: {
      "inquiries.routeHistory.status": "ended"
    }
  },
  {
    $unwind: "$inquiries"
  },
  {
    $match: {
      "inquiries.routeHistory.status": "ended"
    }
  },
  {
    $addFields: {
      "inquiries.routeHistory": {
        $filter: {
          input: "$inquiries.routeHistory",
          cond: {
            $eq: [
              {
                $max: "$inquiries.routeHistory.routeDate"
              },
              "$$this.routeDate"
            ]
          }
        }
      }
    }
  },
  {
    $group: {
      _id: "$_id",
      callId: {
        $first: "$callId"
      },
      caller: {
        $first: "$caller"
      },
      inquiries: {
        $push: "$inquiries"
      }
    }
  }
])
注意:很遗憾,DocumentDB不支持
$map
,在这种情况下,我们可以用