MongoDB compass - 从文档中的 3 个字段及其值中获取具有最大值的字段名称(键)

标签 mongodb aggregation-framework mongodb-compass

我有这个示例 mongodb 文档 -

{
    _id: 5db85ee97d9fb13ead4fc54c
    applId: 5d48f34f7d9fb10ce171f905
    fileId: "dd386cf7-4139-45c2-9853-cbb126621b51"
    job: Object
    country: "US"
    fullName: "abcd xyz"
    htmlWordCount: 2766
    textWordCount: 1867
    rchilliTextWordCount: 2840
    deleted: 0
    dateEntered: 2019-10-29 15:46:49.237
    dateModified: 2019-10-29 15:46:49.237
}

我想在指南针中构建一个查询,以便在输出中包含以下字段 -

{
    _id: 5db85ee97d9fb13ead4fc54c
    country: "US"
    fullName: "abcd xyz"
    htmlWordCount: 2766
    textWordCount: 1867
    rchilliTextWordCount: 2840
    winner: "rchilliTextWordCount"
}

请注意,它有一个名为“winner”的新字段,该字段始终返回具有最大字数的列(共 3 个“htmlWordCount”、“textWordCount”、“rchilliTextWordCount” 列)。这个新列“winner”将在运行时查询时生成。此查询还会根据country = "US" 进行过滤。

如何在 MongoDB Compass 中执行此操作或者聚合管道应该是什么样子?

最佳答案

您可以使用$switch$cond

db.collection.aggregate([
  {
    $match: {
      country: "US"
    }
  },
  {
    $project: {
      country: 1,
      fullName: 1,
      htmlWordCount: 1,
      textWordCount: 1,
      rchilliTextWordCount: 1,
      winner: {
        $switch: {
          branches: [
            {
              case: {
                $and: [
                  {
                    $gt: [
                      "$htmlWordCount",
                      "$textWordCount"
                    ]
                  },
                  {
                    $gt: [
                      "$htmlWordCount",
                      "$rchilliTextWordCount"
                    ]
                  }
                ]
              },
              then: "htmlWordCount"
            },
            {
              case: {
                $and: [
                  {
                    $gt: [
                      "$textWordCount",
                      "$htmlWordCount"
                    ]
                  },
                  {
                    $gt: [
                      "$textWordCount",
                      "$rchilliTextWordCount"
                    ]
                  }
                ]
              },
              then: "textWordCount"
            },
            {
              case: {
                $and: [
                  {
                    $gt: [
                      "$rchilliTextWordCount",
                      "$htmlWordCount"
                    ]
                  },
                  {
                    $gt: [
                      "$rchilliTextWordCount",
                      "$textWordCount"
                    ]
                  }
                ]
              },
              then: "rchilliTextWordCount"
            }
          ],
          default: "No winners"
        }
      }
    }
  }
])

MongoPlayground

关于MongoDB compass - 从文档中的 3 个字段及其值中获取具有最大值的字段名称(键),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58614899/

相关文章:

mongodb - 如何在 MongoDB 聚合中使用 $lookup 作为 INNER JOIN?

javascript - mongodb:匹配数组中的元素并更新

MongoDB Compass显示文档20个限制

mongodb - 有没有办法获取 Mongo Compass 中字段的所有不同值?

json - 如何将 API 请求 JSON 与 MongoDb 中现有的 json 文档进行比较?

node.js - 如何在带有嵌入文档的 MongoDB 中正确使用聚合?

mongodb - 匹配包含 MongoDB 中提供的数组的任意组合的数组字段

java - 从文档中获取多个整数,将它们组合起来,除以值并从集合中获取顶部结果

卡在ubuntu中的Mongodb指南针

mongodb $exists 总是返回 0