我需要在MongoDB中运行这样的查询:
SELECT kills / deaths AS kd FROM stats
我的文档如下:
{
"_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
}
某些文档内部没有stickfight
。我想将kills
除以deaths
,然后检查各轮是否高于15。
我需要5个最好的球员。
我尝试过:
$agg = $coll->aggregate([
['$project' => [
'id' => '$_id'
]],
['$unwind' => '$stickfight'],
['$group' => [
"_id" => '$id',
"kills" => '$stickfight.kills',
'deaths' => '$stickfight.deaths'
]]
])->toArray();
感谢帮助!