您的位置:首页 > 数据库 > Mongodb

mongodb高级查询aggregate使用,主要用于统计分析,筛选排序测试

2017-03-22 18:01 537 查看
1 and or 使用

>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,该字段用来决定是否包含距离字段
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  mongodb 测试 sql
相关文章推荐