开发者社区> 问答> 正文

mongo比较快速的查询方式有大佬说一下嘛?aggregate的速度怎么样?

本问题来自云栖社区【Redis&MongoDB 社区大群】。https://yq.aliyun.com/articles/690084 点击链接欢迎加入社区大社群。

展开
收起
李博 bluemind 2019-04-28 17:45:27 2150 0
1 条回答
写回答
取消 提交回答
  • 1 and or 使用

    >db.col.find({$or:[{key1: value1}, {key2:value2}]})

    1
    2 where使用,和sql一样
    查询已经有回款,但是没有完成回款的订单

    >order
    >db.info.find({'$where': "this.price > this.received_money",status:2}).count()

    1
    2
    3 条件操作符号

    (>) 大于 - $gt
    (<) 小于 - $lt
    (>=) 大于等于 - $gte
    (<= ) 小于等于 - $lte
    db.col.find({likes : {$lt : 150}})

    4 数组嵌套查询

    roster
    db.domain_set.find({})

    { "_id" : ObjectId("55cdb554b9518f0121a9870f"), "did" : NumberLong(75707), "hide_account" : 0, "pds" : { "details" : [ { "key" : "姓名", "type" : 0, "check" : 1 }, { "key" : "性别", "type" : 0, "check" : 1 }, { "key" : "联系方式", "type" : 0, "check" : 1 }, { "key" : "部门", "type" : 0, "check" : 1 }, { "key" : "职位", "type" : 0, "check" : 1 }, { "key" : "工号", "type" : 0 }, { "key" : "邮箱", "type" : 0 }, { "key" : "地址", "type" : 0 }, { "key" : "生日", "type" : 1 }, { "key" : "籍贯", "type" : 1 }, { "key" : "民族", "type" : 1 }, { "key" : "身份证号", "type" : 1, "check" : 1 }, { "key" : "婚姻状况", "type" : 1 }, { "key" : "子女", "type" : 1, "check" : 1 }, { "key" : "家庭住址", "type" : 1 }, { "key" : "紧急联系人", "type" : 1 }, { "key" : "紧急联系电话", "type" : 1 }, { "key" : "毕业日期", "type" : 1 }, { "key" : "入职日期", "type" : 1, "check" : 1 }, { "key" : "111", "type" : 3, "show_name" : "111", "check" : 1 }, { "key" : "222", "type" : 3, "show_name" : "222" }, { "key" : "333", "type" : 3, "show_name" : "333", "check" : 1 } ], "key_alloc" : 100 }, "udversion" : 50 }
    { "_id" : ObjectId("55d693c2b9518f0121ada57f"), "did" : NumberLong(11111), "hide_account" : 0, "udversion" : 1 }
    
    db.domain_set.find({"pds.details":{"$elemMatch":{"show_name" : "1111"}}})
    db.test.find({"pds.details.19.key":"1111"})
    

    5 只显示某几个字段
    查询did=10000的公司下面的订单,只显示price和order_id字段

    order
    db.info.find({did:10000},{price:1,order_id:1})

    6 分页查询–limit和skip
    查询did=10000的已经确认的订单,按照order_id(最新创建时间排序)

    order
    显示前15个,第一页
    db.info.find({did:10000,status:2},{order_id:1,price:1}).sort({order_id:-1}).limit(15)
    加载16到30页,第二页
    db.info.find({did:10000,status:2},{order_id:1,price:1}).sort({order_id:-1}).limit(15).skip(15)

    7 aggregate使用,相当于shell里面的”|”
    上面的几乎全部可以用aggregate进行查询
    与sql对应关系

    sql mongodb

    WHERE $match //match里面可以用and,or,以及逻辑判断,但是好像不能用where
    GROUP BY $group
    HAVING $match
    SELECT $project
    ORDER BY $sort
    LIMIT $limit
    SUM() $sum
    COUNT() $sum

    特殊:暂时还没有用到
    $unwind 将数组元素拆分为独立字段
    $goNear 会返回一些坐标值,这些值以按照距离指定点距离由近到远进行排序

    数字运算符
    $multiply 乘
    $add 加
    $subtract 减
    $mod 取模
    $divide 除

    order项目中使用:
    1 统计某一段时间的订单总额和订单数量:
    db.info.aggregate([

    {
     $match:{
            did:10000,
            status:2,
            ordered_time:{$gt:1488297600000,$lt:1490976000000}
            }
    },
    {
    $group: {
        _id: null,
        total: { $sum: "$price" },
        order_num:{$sum:1}
            }
    }

    ])
    2 按照未回款的金额大小排序,同时显示订单金额,未回款金额
    db.info.aggregate([

    {
     $match:{
            did:10000,
            status:2,
            ordered_time:{$gt:1488297600000,$lt:1490976000000}
            }
    },
    {
    $project:{
        price:1,
        did:1,
        order_id:1,
        notpay:{$subtract:["$price","$received_money"]}
    }
    },
    {
    $sort:{
        notpay:-1
    }
    }

    ])

    8 其他实例:
    2 统计已经完成回款的订单

    db.info.find({ $or:[{'$where': "this.price <= this.received_money"},{price:0}],

                    did:10000,
                    status:2,
                    ordered_time:{$gt:1488297600000,$lt:1490976000000}
                    },
                    {price:1}).sort({price:-1})

    3 查询所有未完成回款的订单
    1

    db.info.find({ $or:[{'$where': "this.price > this.received_money"},{received_money:{$exists:false}}],

                    did:10000,
                    status:2,
                    ordered_time:{$gt:1488297600000,$lt:1490976000000}
                    },
                    {price:1}).sort({price:-1})
    
    2019-07-17 23:34:31
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Phoenix Search Index 功能与应用场景介绍 立即下载
Data as a Service - 数据即服务 -- MongoDB⾼级应⽤模式 立即下载
MongoShake -- Multi Active-Active and Cross-Region Disaster Recoverable MongoDB Service 立即下载