Reputation: 13
{
"_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"
}
}]
}]
}
I have the following mongodb schema for a gaming system.I want to write a query to find maximum score in each game. Not able to figure out what to do!
Upvotes: 1
Views: 1101
Reputation: 36104
$map
to iterate loop of achievement.players.score
and get max from array of array$max
to get maximum number from arraydb.collection.aggregate([
{
$addFields: {
maxScore: {
$max: {
$map: {
input: "$achievement.players.score",
in: { $max: "$$this" }
}
}
}
}
}
])
Upvotes: 2
Reputation: 59436
Not fully clear what you mean by max score in each game (what is a "game") but a simple solution is this:
db.collection.aggregate([
{
$addFields: {
max_score: {
$max: {
$max: "$achievement.players.score"
}
}
}
}
])
Upvotes: 0