MongoDB compass - 从文档中的 3 个字段及其值中获取具有最大值的字段名称(键)
MongoDB compass - Get the field name (key) with the max value, from 3 fields and their values in a document
我有这个示例 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" 的新字段,它总是 returns 具有最大字数的列(共 3 "htmlWordCount", "textWordCount", "rchilliTextWordCount"
列)。这个新列 "winner"
将在运行时查询时生成。此查询也在 country = "US"
.
上过滤
如何在 MongoDB Compass 中执行此操作,或者聚合管道应该是什么样的?
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"
}
}
}
}
])
这是获取结果的另一种方法:
- 获取文档的字段名称及其值
- 查找名称在
[ "htmlWordCount", "textWordCount", "rchilliTextWordCount" ]
中的字段的最大值。
一般来说,从数组中求最大值是一种归约;所以我在这种情况下使用了 $reduce
。注意代码更简单。如果您想添加另一个字段来计算最大值,只需将其添加到数组即可。
db.winner.aggregate([
{ $match: { country: "US"} },
{ $addFields: { fieldNameValues: { "$objectToArray": "$$ROOT" } } },
{ $project: { _id: 1, country: 1, fullName: 1, htmlWordCount: 1, textWordCount: 1, rchilliTextWordCount: 1,
winner: {
$reduce: {
input: "$fieldNameValues",
initialValue: { },
in: {
$cond: [
{ $and: [
{ $in: [ "$$this.k", [ "htmlWordCount", "textWordCount", "rchilliTextWordCount" ] ] },
{ $gt: [ "$$this.v", "$$value.v"] } ]
},
"$$this",
"$$value"
]
}
}
}
} },
{ $addFields: { winner: "$winner.k" } }
] )
[编辑添加]
示例数据和结果:
{
"_id" : 1,
"fileId" : "dd386cf7-4139-45c2-9853-cbb126621b51",
"job" : { },
"country" : "US",
"fullName" : "abcd xyz",
"htmlWordCount" : 2766,
"textWordCount" : 1867,
"rchilliTextWordCount" : 2840
}
{
"_id" : 2,
"fileId" : "dd386cf7-4139-45c2-9853-cbb126621b51",
"job" : { },
"country" : "US",
"fullName" : "lmn opqrs",
"htmlWordCount" : 5,
"textWordCount" : 9,
"rchilliTextWordCount" : 2
}
输出:
{
"_id" : 1,
"country" : "US",
"fullName" : "abcd xyz",
"htmlWordCount" : 2766,
"textWordCount" : 1867,
"rchilliTextWordCount" : 2840,
"winner" : "rchilliTextWordCount"
}
{
"_id" : 2,
"country" : "US",
"fullName" : "lmn opqrs",
"htmlWordCount" : 5,
"textWordCount" : 9,
"rchilliTextWordCount" : 2,
"winner" : "textWordCount"
}
我有这个示例 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" 的新字段,它总是 returns 具有最大字数的列(共 3 "htmlWordCount", "textWordCount", "rchilliTextWordCount"
列)。这个新列 "winner"
将在运行时查询时生成。此查询也在 country = "US"
.
如何在 MongoDB Compass 中执行此操作,或者聚合管道应该是什么样的?
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"
}
}
}
}
])
这是获取结果的另一种方法:
- 获取文档的字段名称及其值
- 查找名称在
[ "htmlWordCount", "textWordCount", "rchilliTextWordCount" ]
中的字段的最大值。
一般来说,从数组中求最大值是一种归约;所以我在这种情况下使用了 $reduce
。注意代码更简单。如果您想添加另一个字段来计算最大值,只需将其添加到数组即可。
db.winner.aggregate([
{ $match: { country: "US"} },
{ $addFields: { fieldNameValues: { "$objectToArray": "$$ROOT" } } },
{ $project: { _id: 1, country: 1, fullName: 1, htmlWordCount: 1, textWordCount: 1, rchilliTextWordCount: 1,
winner: {
$reduce: {
input: "$fieldNameValues",
initialValue: { },
in: {
$cond: [
{ $and: [
{ $in: [ "$$this.k", [ "htmlWordCount", "textWordCount", "rchilliTextWordCount" ] ] },
{ $gt: [ "$$this.v", "$$value.v"] } ]
},
"$$this",
"$$value"
]
}
}
}
} },
{ $addFields: { winner: "$winner.k" } }
] )
[编辑添加]
示例数据和结果:
{
"_id" : 1,
"fileId" : "dd386cf7-4139-45c2-9853-cbb126621b51",
"job" : { },
"country" : "US",
"fullName" : "abcd xyz",
"htmlWordCount" : 2766,
"textWordCount" : 1867,
"rchilliTextWordCount" : 2840
}
{
"_id" : 2,
"fileId" : "dd386cf7-4139-45c2-9853-cbb126621b51",
"job" : { },
"country" : "US",
"fullName" : "lmn opqrs",
"htmlWordCount" : 5,
"textWordCount" : 9,
"rchilliTextWordCount" : 2
}
输出:
{
"_id" : 1,
"country" : "US",
"fullName" : "abcd xyz",
"htmlWordCount" : 2766,
"textWordCount" : 1867,
"rchilliTextWordCount" : 2840,
"winner" : "rchilliTextWordCount"
}
{
"_id" : 2,
"country" : "US",
"fullName" : "lmn opqrs",
"htmlWordCount" : 5,
"textWordCount" : 9,
"rchilliTextWordCount" : 2,
"winner" : "textWordCount"
}