分组 mongodb 聚合框架后无法对文档进行排序

Unable to sort documents after grouping mongodb aggregation framework

所以我正在尝试按插入日期对用户的通知数组进行排序(我希望最新的放在最前面)但它似乎不起作用我是否遗漏了什么?

这是数据模板:

{
  _id: "628ceeae3df06d49419f0bb4",
  name: "John",
  notifications: [
    {_id: "someIdA", details: "xyz", dateTime: "1653321337762"},
    {_id: "someIdB", details: "jkl", dateTime: "1653321337762"}
    {_id: "someIdC", details: "abc", dateTime: "1653321321323"}
    {_id: "someIdD", details: "lmn", dateTime: "1653123412341"}
  ]
}

以及我正在尝试的聚合管道:

const foundUser = await users.aggregate([
    {
      $match: {
        _id: mongoose.Types.ObjectId(userId)
      }
    },
    {
      $project: {
        notifications: 1,
        _id: 0
      }
    },
    {
      $sort: {
        _id: -1
      }
    }
  ])

首先展开通知数组,按通知日期时间键对文档排序,然后按用户分组。

这是 mongo shell 查询:

  db.collection.aggregate([
  {
    $match: {
      _id: "628ceeae3df06d49419f0bb4"
    }
  },
  {
    $unwind: "$notifications"
  },
  {
    $sort: {
      "notifications.dateTime": -1
    }
  },
  {
    $group: {
      _id: "$_id",
      notifications: {
        $push: "$notifications"
      }
    }
  },
  {
    "$project": {
      "notifications": 1,
      "_id": 0
    }
  }
])

这里是工作linkMongodb Playground