[英]Find the value inside the array of objects that include children from the array of objects
[英]How to find maximum value from array inside array of objects in mongodb
{
"_id": {
"$__id": "608028497a90cf06c02b1083"
},
"name": "Player Unknown's Batteground Mobile",
"publisher_detail": "Bluehole Corporation",
"release_date": {
"$date": "2017-03-26T18:30:00.000Z"
},
"version": "1.2.0",
"genre": "action",
"rating": 100,
"achievement": [{
"name": "Ace",
"players": [{
"player_name": "notAplayer",
"score": 60,
"date_of_achievement": {
"$date": "2019-02-14T18:30:00.000Z"
},
{
"player_name": "notAplayer2",
"score": 92,
"date_of_achievement": {
"$date": "2020-04-14T18:30:00.000Z"
}
}]
}]
}
我有一个游戏系统的以下 mongodb 架构。我想写一个查询来找到每场比赛的最高分。 不知道该怎么办!
不完全清楚每场比赛的最高得分是什么意思(什么是“比赛”),但一个简单的解决方案是:
db.collection.aggregate([
{
$addFields: {
max_score: {
$max: {
$max: "$achievement.players.score"
}
}
}
}
])
$map
迭代achievement.players.score
.players.score 循环并从数组数组中获取最大值$max
从数组中获取最大数db.collection.aggregate([
{
$addFields: {
maxScore: {
$max: {
$map: {
input: "$achievement.players.score",
in: { $max: "$$this" }
}
}
}
}
}
])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.