码迷,mamicode.com
首页 > 数据库 > 详细

MongoDB使用: 条件操作,排序,索引,分页查询,聚合函数

时间:2020-04-26 11:22:42      阅读:71      评论:0      收藏:0      [点我收藏+]

标签:tom   记录   call   automatic   mongodb使用   aggregate   and   rip   iter   

#MongoDB使用: 条件操作,排序,索引,分页查询,聚合函数
##mongodb条件查询
> use study
switched to db study
> db.student.insert({"name":"张三","age":18,"score":100,"sex":1})
WriteResult({ "nInserted" : 1 })
> db.student.insert({"name":"李四","age":20,"score":90,"sex":1})
WriteResult({ "nInserted" : 1 })
> db.student.insert({"name":"小红","age":19,"score":100,"sex":0})
WriteResult({ "nInserted" : 1 })
> db.student.insert({"name":"小明","age":19,"score":95,"sex":1})
WriteResult({ "nInserted" : 1 })
首先要创建一个study数据库,并创建student集合,存入四条数据。
```shell script
1. 查询年龄大于19的学生。‘>‘==>‘gt‘ // ‘>=‘==>‘gte‘
> db.student.find({"age":{$gt:19}})
{ "_id" : ObjectId("5ea502960cf4f3a59d7d9850"), "name" : "李四", "age" : 20, "score" : 90, "sex" : 1 }
2. 查询成绩小于等于95的学生 ‘<‘==>‘lt‘ // ‘<=‘==>‘lte‘
> db.student.find({"score":{$lte: 95}})
{ "_id" : ObjectId("5ea502960cf4f3a59d7d9850"), "name" : "李四", "age" : 20, "score" : 90, "sex" : 1 }
{ "_id" : ObjectId("5ea502c70cf4f3a59d7d9852"), "name" : "小明", "age" : 19, "score" : 95, "sex" : 1 }
3. 查询年龄在18-20之间的。 where age>18 and age <20
> db.student.find({"age":{$lt:20,$gt:18}})
{ "_id" : ObjectId("5ea502ae0cf4f3a59d7d9851"), "name" : "小红", "age" : 19, "score" : 100, "sex" : 0 }
{ "_id" : ObjectId("5ea502c70cf4f3a59d7d9852"), "name" : "小明", "age" : 19, "score" : 95, "sex" : 1 }
```
#分页查询
db.collection.find(query).limit(number)
查询两条数据
```shell script
> db.student.find()
{ "_id" : ObjectId("5ea502830cf4f3a59d7d984f"), "name" : "张三", "age" : 18, "score" : 100, "sex" : 1 }
{ "_id" : ObjectId("5ea502960cf4f3a59d7d9850"), "name" : "李四", "age" : 20, "score" : 90, "sex" : 1 }
{ "_id" : ObjectId("5ea502ae0cf4f3a59d7d9851"), "name" : "小红", "age" : 19, "score" : 100, "sex" : 0 }
{ "_id" : ObjectId("5ea502c70cf4f3a59d7d9852"), "name" : "小明", "age" : 19, "score" : 95, "sex" : 1 }
> db.student.find().limit(2)
{ "_id" : ObjectId("5ea502830cf4f3a59d7d984f"), "name" : "张三", "age" : 18, "score" : 100, "sex" : 1 }
{ "_id" : ObjectId("5ea502960cf4f3a59d7d9850"), "name" : "李四", "age" : 20, "score" : 90, "sex" : 1 }
```
##跳过skip
查询第2-3条数据
```shell script
> db.student.find().limit(2).skip(1)
{ "_id" : ObjectId("5ea502960cf4f3a59d7d9850"), "name" : "李四", "age" : 20, "score" : 90, "sex" : 1 }
{ "_id" : ObjectId("5ea502ae0cf4f3a59d7d9851"), "name" : "小红", "age" : 19, "score" : 100, "sex" : 0 }
```
通过使用skip和limit我们可以进行分页查询。先在集合中插入10条记录。
```shell script
> db.student.find()
{ "_id" : ObjectId("5ea502830cf4f3a59d7d984f"), "name" : "张三", "age" : 18, "score" : 100, "sex" : 1 }
{ "_id" : ObjectId("5ea502960cf4f3a59d7d9850"), "name" : "李四", "age" : 20, "score" : 90, "sex" : 1 }
{ "_id" : ObjectId("5ea502ae0cf4f3a59d7d9851"), "name" : "小红", "age" : 19, "score" : 100, "sex" : 0 }
{ "_id" : ObjectId("5ea502c70cf4f3a59d7d9852"), "name" : "小明", "age" : 19, "score" : 95, "sex" : 1 }
{ "_id" : ObjectId("5ea508010cf4f3a59d7d9853"), "name" : "王二", "age" : 22, "score" : 88, "sex" : 1 }
{ "_id" : ObjectId("5ea508140cf4f3a59d7d9854"), "name" : "马六", "age" : 21, "score" : 88, "sex" : 0 }
{ "_id" : ObjectId("5ea508280cf4f3a59d7d9855"), "name" : "小芳", "age" : 18, "score" : 88, "sex" : 0 }
{ "_id" : ObjectId("5ea5083a0cf4f3a59d7d9856"), "name" : "小李", "age" : 18, "score" : 93, "sex" : 1 }
{ "_id" : ObjectId("5ea5084a0cf4f3a59d7d9857"), "name" : "小张", "age" : 18, "score" : 98, "sex" : 1 }
{ "_id" : ObjectId("5ea5085d0cf4f3a59d7d9858"), "name" : "小强", "age" : 22, "score" : 70, "sex" : 1 }
```
查询第3页数据,每页3条。 db.collection.find().limit(3).skip((3-1)*3) <==> limit (x-1)*3, 3;
```shell script
> db.student.find().skip(2*3).limit(3)
{ "_id" : ObjectId("5ea508280cf4f3a59d7d9855"), "name" : "小芳", "age" : 18, "score" : 88, "sex" : 0 }
{ "_id" : ObjectId("5ea5083a0cf4f3a59d7d9856"), "name" : "小李", "age" : 18, "score" : 93, "sex" : 1 }
{ "_id" : ObjectId("5ea5084a0cf4f3a59d7d9857"), "name" : "小张", "age" : 18, "score" : 98, "sex" : 1 }
```
##排序
还是上面的集合,按照年龄升序排序。 db.collection.find(query).sort({key:1}) 1表示升序,-1表示降序
```shell script
> db.student.find().sort({age:1})
{ "_id" : ObjectId("5ea502830cf4f3a59d7d984f"), "name" : "张三", "age" : 18, "score" : 100, "sex" : 1 }
{ "_id" : ObjectId("5ea508280cf4f3a59d7d9855"), "name" : "小芳", "age" : 18, "score" : 88, "sex" : 0 }
{ "_id" : ObjectId("5ea5083a0cf4f3a59d7d9856"), "name" : "小李", "age" : 18, "score" : 93, "sex" : 1 }
{ "_id" : ObjectId("5ea5084a0cf4f3a59d7d9857"), "name" : "小张", "age" : 18, "score" : 98, "sex" : 1 }
{ "_id" : ObjectId("5ea502ae0cf4f3a59d7d9851"), "name" : "小红", "age" : 19, "score" : 100, "sex" : 0 }
{ "_id" : ObjectId("5ea502c70cf4f3a59d7d9852"), "name" : "小明", "age" : 19, "score" : 95, "sex" : 1 }
{ "_id" : ObjectId("5ea502960cf4f3a59d7d9850"), "name" : "李四", "age" : 20, "score" : 90, "sex" : 1 }
{ "_id" : ObjectId("5ea508140cf4f3a59d7d9854"), "name" : "马六", "age" : 21, "score" : 88, "sex" : 0 }
{ "_id" : ObjectId("5ea508010cf4f3a59d7d9853"), "name" : "王二", "age" : 22, "score" : 88, "sex" : 1 }
```
##索引 createIndex(key,options)
```shell script
> db.student.createIndex({"score":1})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
```
{"score":1},表示创建score的索引,升序排序。还可以写复合索引{"score":1,"age":-1}
## 聚合
查询男女生数量,男为1,女为0
```shell script
> db.student.aggregate([{$group:{"_id":"$sex",num_tutorial:{$sum:1}}}])
{ "_id" : 0, "num_tutorial" : 3 }
{ "_id" : 1, "num_tutorial" : 7 }
```

MongoDB使用: 条件操作,排序,索引,分页查询,聚合函数

标签:tom   记录   call   automatic   mongodb使用   aggregate   and   rip   iter   

原文地址:https://www.cnblogs.com/Zs-book1/p/12777467.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!