I need to run a query like this in MongoDB:
SELECT kills / deaths AS kd FROM stats
My documents look like this:
{
"_id": ObjectId("5b6e8c66568a4e65461a5893"),
"uuid": "889fd936-1a1e-31ac-b1fc-1ca34576301d",
"coins": 146,
"onlinetime": 988,
"logins": 540,
"messages": 31,
"rounds": 0,
"lobby": {
"balloons": {
"skeleton": true,
"creeper": true,
"enderdragon": true,
"wither": true
},
"jumpandrun": true
},
"stickfight": {
"inventory": {
"stick": 0,
"blocks": 1
},
"rounds": 26,
"kills": 219,
"deaths": 156
}
Some of the documents don't have stickfight
inside. I want to divide kills
with deaths
and check if the rounds are higher than 15.
I need the 5 best players.
I tried this:
$agg = $coll->aggregate([
['$project' => [
'id' => '$_id'
]],
['$unwind' => '$stickfight'],
['$group' => [
"_id" => '$id',
"kills" => '$stickfight.kills',
'deaths' => '$stickfight.deaths'
]]
])->toArray();
Thanks for help!