This is one out of ~10 million rows in my database:
{
"_id" : ObjectId("58f569159f809c49ffc5cdbb"),
"date" : ISODate("2017-03-21T09:10:07.686Z"),
"p1" : {
"_id" : 1765906,
"deck" : [
33,
25,
59,
38,
62,
3,
33,
57
],
"crowns" : 3
},
"p2" : {
"_id" : 2520156,
"deck" : [
25,
69,
86,
8,
44,
61,
69,
50
],
"crowns" : 2
}
}
It is a log of a game battle. The deck is an array which consists of 8 different cards. I am trying to find the card which has the most wins. A winner can be determined by comparing the crowns (I managed to select all documents where player 1 has won).
What I am trying to achieve:
Unfortunately I wasn't able to perform a group query which would return what I am looking for (the card with the most wins).
I am also trying to find the most successful deck (quantity of wins is enough - the order of the cards specified in that array should be ignored).
What I have tried, but returned an empty error after some time:
db.battle_logs.aggregate([
{
$addFields: {
"player1won": { "$cmp": [ "$p1.crowns", "$p2.crowns" ] }
}
},
{ $match: { "player1won": 1 }},
{
$group:
{
_id: "$p1.deck",
count: { $sum: 1 }
}
}
], {
allowDiskUse:true,
cursor:{}
})
I expected to get all deck combinations grouped by its quantity of wins (only player1 wins considered)