【问题标题】:How to filter an array of objects in mongoose by date field only selecting the most recent date如何按日期字段过滤猫鼬中的对象数组,仅选择最近的日期
【发布时间】:2021-08-10 06:20:41
【问题描述】:

我正在尝试过滤 MongoDB 上用户集合中的对象数组。这个特定集合的结构如下所示:

name: "John Doe"
email: "john@doe.com"
progress: [
        {
         _id : ObjectId("610be25ae20ce4872b814b24")
         challenge: ObjectId("60f9629edd16a8943d2cab9b")
         date_unlocked: 2021-08-05T12:15:32.129+00:00
         completed: true
         date_completed: 2021-08-06T12:15:32.129+00:00
        }
        {
         _id : ObjectId("611be24ae32ce4772b814b32")
         challenge: ObjectId("60g6723efd44a6941l2cab81")
         date_unlocked: 2021-08-06T12:15:32.129+00:00
         completed: true
         date_completed: 2021-08-07T12:15:32.129+00:00
        }
]
date: 2021-08-04T13:06:34.129+00:00

如何使用 mongoose 查询数据库以仅返回具有最新 'date_unlocked' 的挑战?

我试过了:User.findById(req.user.id).select('progress.challenge progress.date_unlocked').sort({'progress.date_unlocked': -1}).limit(1);

但不是返回带有最近“date_unlocked”的单个挑战,而是返回整个用户进度数组。

任何帮助将不胜感激,在此先感谢您!

【问题讨论】:

标签: mongodb mongoose


【解决方案1】:

你可以试试这个。

db.collection.aggregate([
  {
    "$unwind": {
      "path": "$progress"
    }
  },
  {
    "$sort": {
      "progress.date_unlocked": -1
    }
  },
  {
    "$limit": 1
  },
  {
    "$project": {
      "_id": 0,
      "latestChallenge": "$progress.challenge"
    }
  }
])

Test the code here

另一种解决方案是在该数组中使用$reduce

db.collection.aggregate([
  {
    "$addFields": {
      "latestChallenge": {
        "$arrayElemAt": [
          {
            "$reduce": {
              "input": "$progress",
              "initialValue": [
                "0",
                ""
              ],
              "in": {
                "$let": {
                  "vars": {
                    "info": "$$value",
                    "progress": "$$this"
                  },
                  "in": {
                    "$cond": [
                      {
                        "$gt": [
                          "$$progress.date_unlocked",
                          {
                            "$arrayElemAt": [
                              "$$info",
                              0
                            ]
                          }
                        ]
                      },
                      [
                        {
                          "$arrayElemAt": [
                            "$$info",
                            0
                          ]
                        },
                        "$$progress.challenge"
                      ],
                      "$$info"
                    ]
                  }
                }
              }
            }
          },
          1
        ]
      }
    }
  },
  {
    "$project": {
      "_id": 0,
      "latestChallenge": 1
    }
  },
])

Test the code here

Mongoose 可以使用原始 MQL,因此您可以使用它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-15
    • 2020-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多