V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐工具
RoboMongo
推荐书目
50 Tips and Tricks for MongoDB Developers
Related Blogs
Snail in a Turtleneck
coderabbit
V2EX  ›  MongoDB

mysql 转写 mongo 语句.不知道算复杂不复杂!

  •  
  •   coderabbit · 2019-11-11 21:38:18 +08:00 · 14810 次点击
    这是一个创建于 1599 天前的主题,其中的信息可能已经有所发展或是发生改变。

    原 sql 语句

    SELECT `*`,GROUP_CONCAT(`invoice_number`) as `invoice_number_all`,ROUND(SUM(`invoice_amount`)/100,2) as `invoice_amount_all`,ROUND(SUM(`premium_amount`)/100,2) as `premium_amount_all`  FROM `t_invoice` WHERE `id` != 0  GROUP BY `insurer_id`,`region_id`,`invoice_company_id`,`data_type`,`invoice_date`,`invoice_remark` ORDER BY `invoice_date` DESC LIMIT 0,15
    

    刚开始用 mongo 数据库很多操作不会写。简单的还会,这是 mysql 查询分组后的结果,转换到 mongo 没有写正确导致查询不出数据。想问问这样的 sql 语句转 mongo 合适吗?还是要怎么处理呢!

    db.t_invoice.aggregate({ $group: { _id: "$insurer_id"} })
    我这样写只能一个,多个我怎么写都报错,查询不出结果。而且字段名也不是我想要的 我把 _id 改成字段就开始报错!
    
    5 条回复    2019-11-12 09:26:28 +08:00
    Hieast
        1
    Hieast  
       2019-11-11 22:38:25 +08:00 via iPhone
    mongo 里面到处是 json,那个 id 也要写成 json
    coderabbit
        2
    coderabbit  
    OP
       2019-11-11 22:40:49 +08:00
    @Hieast

    db.t_invoice.aggregate(
    {
    "$group": {
    _id: {
    insurer_id:"$insurer_id",
    region_id: "$region_id",
    invoice_company_id:"$invoice_company_id",
    data_type: "$data_type",
    invoice_date: "$invoice_date",
    invoice_remark: "$invoice_remark",
    },
    count:{$sum:1}
    }
    }
    )

    这样子我看分组是出来了!但是还不知道如何加字段,count kv 我删除后就没有统计结果了。_id 我一改名就报错!我想它跟 mysql 查出来一样的结果!
    Hieast
        3
    Hieast  
       2019-11-12 01:45:18 +08:00 via iPhone
    搜下 pipe
    mmdsun
        4
    mmdsun  
       2019-11-12 09:04:16 +08:00 via Android
    你需要一个转换工具。
    用 studio 3t。直接写 SQL 可转换成 MongoDB shell,Java 代码,Python 代码。
    catinsides
        5
    catinsides  
       2019-11-12 09:26:28 +08:00
    aggregate 里得是数组,db.t_invoice.aggregate([{...}, {...}]
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4499 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 05:34 · PVG 13:34 · LAX 22:34 · JFK 01:34
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.