douang2297 2018-08-25 17:10
浏览 39

我怎么能把这个MySql查询翻译成MongoDB?

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.

https://pastebin.com/AqscMYSp

I tried this:

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

Thanks for help!

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
    • ¥15 qt6.6.3 基于百度云的语音识别 不会改
    • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
    • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
    • ¥15 lingo18勾选global solver求解使用的算法
    • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
    • ¥20 测距传感器数据手册i2c
    • ¥15 RPA正常跑,cmd输入cookies跑不出来
    • ¥15 求帮我调试一下freefem代码
    • ¥15 matlab代码解决,怎么运行