mongodb高级查询aggregate使用,主要用于统计分析,筛选排序测试
2017-03-22 18:01
537 查看
1 and or 使用
2 where使用,和sql一样
查询已经有回款,但是没有完成回款的订单
3 条件操作符号
4 数组嵌套查询
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对应关系
8 其他实例:
2 统计已经完成回款的订单
参考文档:
http://www.cnblogs.com/shanyou/p/3494854.html
https://docs.mongodb.com/manual/core/aggregation-pipeline/
暂时不用看,附近的客户统计使用
注意: 1.使用$goNear只能在管道处理的开始第一个阶段进行
>db.col.find({$or:[{key1: value1}, {key2:value2}]})
2 where使用,和sql一样
查询已经有回款,但是没有完成回款的订单
>order >db.info.find({'$where': "this.price > this.received_money",status:2}).count()
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: " c606 $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 查询所有未完成回款的订单
>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})
参考文档:
http://www.cnblogs.com/shanyou/p/3494854.html
https://docs.mongodb.com/manual/core/aggregation-pipeline/
暂时不用看,附近的客户统计使用
$goNear使用方法 db.places.aggregate([ { $geoNear: { near: [40.724, -73.997], distanceField: "dist.calculated", maxDistance: 0.008, query: { type: "public" }, includeLocs: "dist.location", uniqueDocs: true, num: 5 } } ])
注意: 1.使用$goNear只能在管道处理的开始第一个阶段进行
2.必须指定distanceField,该字段用来决定是否包含距离字段
相关文章推荐
- bug统计分析---严重级别( 统计分析的目的: 总结和分析每个季度在测试团队中所处的级别,用于改进之后的工作)
- MongoDB的使用学习之(六)MongoDB的高级查询之条件操作符
- 数据库SQL中对查询结果排序排列序号编号,Oracle分析函数 rank,dense_rank,row_number使用和区别
- 使用python 分析统计nginx访问日志ip次数并且排序
- information_schema中Innodb相关表用于分析sql查询锁的使用情况介绍
- 开始VS 2012中LightSwitch系列的第4部分:太多信息了!使用查询来排序和筛选数据
- hibernate使用groupProperty,sqlGroupProjection查询,为sqlGroupProjection设置别名,用于排序
- information_schema中Innodb相关表用于分析sql查询锁的使用情况介绍
- Excel在统计分析中的应用—第三章—数据库统计函数与数据透视表-Part1-(数据查询与筛选、分类汇总)
- python使用mongodb查询的排序
- 使用aggregate在MongoDB中查询重复数据记录的方法
- mongodb 在包含or条件查询的时候,尽量不使用排序
- spring data mongodb高级查询语句的使用(网上基本没资料)
- hibernate使用groupProperty,sqlGroupProjection查询,为sqlGroupProjection设置别名,用于排序
- Nosql Mongodb之旅(8)—MongoDB高级查询の游标使用
- 61.Oracle数据库SQL开发之 高级查询——使用分析函数之反百分点函数
- 62.Oracle数据库SQL开发之 高级查询——使用分析函数之窗口函数
- information_schema中Innodb相关表用于分析sql查询锁的使用情况介绍
- mongoDB 性能优化:如何使用普通查询语句替代 aggregate 操作?