如何将这个MySql查询转换为MongoDB?

时间:2018-08-25 17:10:01

标签: php mongodb mongodb-query

我需要在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个最好的球员。

https://pastebin.com/AqscMYSp

我尝试过:

$agg = $coll->aggregate([
        ['$project' => [
            'id' => '$_id'
        ]],
        ['$unwind' => '$stickfight'],
        ['$group' => [
            "_id" => '$id',
            "kills" => '$stickfight.kills',
            'deaths' => '$stickfight.deaths'
        ]]
    ])->toArray();

感谢帮助!

0 个答案:

没有答案