【问题标题】:How to check all the documents in a MongoDB database for the latest value of a particular field using aggregation?如何使用聚合检查 MongoDB 数据库中的所有文档以获取特定字段的最新值?
【发布时间】:2020-03-07 07:00:10
【问题描述】:

我是 MongoDB 新手,正在使用它处理 NodeJS 代码。

我需要使用聚合函数来拉取每本书的最新book_version。

这是我的数据库的外观:

[
  {
    "book_id": "ab12nld”,
    "book_version": "0”,
    "author": “Sam”,
    “name”: “Sample Book”,
    “comments”: “Done”
  },
  {
    "book_id": "ab12nld”,
    "book_version": "1",
    "author": "Martin",
    "name": "Sample Book",
    “comments”: “In Progress”
  },
  {
    "book_id": "ab12nld”,
    "book_version": "2",
    "author": "Roy",
    "name": "Sample Book",
    “comments”: “To-Do”
  }
]
[
  {
    "book_id": "bcj123n”,
    "book_version": "0”,
    "author": “Don”,
    “name”: “Another Book”,
    “comments”: “Done”
  },
  {
    "book_id": "bcj123n”,
    "book_version": "1",
    "author": "Ray",
    "name": "Another Book",
    “comments”: “In Progress”
  },
  {
    "book_id": "bcj123n”,
    "book_version": "2",
    "author": "Max",
    "name": "Another Book",
    “comments”: “To-Do”
  }
]
[
  {
    "book_id": "k23d1d3”,
    "book_version": "0”,
    "author": “Sunny”,
    “name”: “New Book”,
    “comments”: “Done”
  },
  {
    "book_id": "k23d1d3”,
    "book_version": "1",
    "author": "Archer",
    "name": "New Book",
    “comments”: “In Progress”
  }
]
[
  {
    "book_id": "o902f3s”,
    "book_version": "0”,
    "author": “Joy”,
    “name”: “Nice Book”,
    “comments”: “Done”
  },
  {
    "book_id": "o902f3s”,
    "book_version": "1",
    "author": "James",
    "name": "Nice Book",
    “comments”: “In Progress”
  },
{
    "book_id": "o902f3s”,
    "book_version": "2”,
    "author": “Pam”,
    “name”: “Nice Book”,
    “comments”: “Done”
  },
  {
    "book_id": "o902f3s”,
    "book_version": "3",
    "author": "Jonah",
    "name": "Nice Book",
    “comments”: “Completed”
  }
]

因此,我应该为每个 book_id 以及整个对象获得 book_version 的最大值:

{
    "book_id": "ab12nld”,
    "book_version": "2",
    "author": "Roy",
    "name": "Sample Book",
    “comments”: “To-Do”
  },
{
    "book_id": "bcj123n”,
    "book_version": "2",
    "author": "Max",
    "name": "Another Book",
    “comments”: “To-Do”
  },
{
    "book_id": "k23d1d3”,
    "book_version": "1",
    "author": "Archer",
    "name": "New Book",
    “comments”: “In Progress”
  },
  {
    "book_id": "o902f3s”,
    "book_version": "3",
    "author": "Jonah",
    "name": "Nice Book",
    “comments”: “Completed”
  }

【问题讨论】:

    标签: mongodb mongoose aggregation-framework aggregation


    【解决方案1】:

    您可以使用$sort 和$group 的组合来达到预期的效果:

    db.collection.aggregate([
      {
        $sort: {
          // Sort by the book_version from highest to lowest 
          book_version: -1
        }
      },
      {
        $group: {
          // Group the documents by their book_id
          _id: "$book_id",
           book_version: {
            // The first book_version is the highest book version
            // The same thing applies to every other first-of other book fields 
            $first: "$book_version"
          },
          book_id: {
            $first: "$book_id"
          },
          author: {
            "$first": "$author"
          },
          name: {
            "$first": "$name"
          },
          comments: {
            "$first": "$comments"
          }
        }
      }
    ])
    

    【讨论】:

      【解决方案2】:

      您可以通过book_id $group 并使用$max 运算符捕获lastVersion。然后你需要$filter属于特定组的所有书籍,并使用$replaceRoot将单个找到的最新版本提升到根级别。

      db.collection.aggregate([
          {
              $group: {
                  _id: "$book_id",
                  lastVersion: { $max: "$book_version" },
                  versions: { $push: "$$ROOT" }
              }
          },
          {
              $replaceRoot: {
                  newRoot: {
                      $let: {
                          vars: {
                              lastVer: {
                                  $filter: { 
                                      input: "$versions", 
                                      cond: { $eq: [ "$$this.book_version", "$lastVersion" ] } 
                                  }
                              }
                          },
                          in: { $arrayElemAt: [ "$$lastVer", 0 ] }
                      }
                  }
              }
          }
      ])
      

      Mongo Playground

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-09-17
        • 1970-01-01
        • 2020-09-03
        • 2019-01-17
        • 2023-02-09
        • 2020-10-02
        • 2021-02-27
        相关资源
        最近更新 更多