mongodb的学习 (2)
1.条件查询
查询姓名为小明的学生 db.local.find({name:'小明'});;
查询英语成绩大于90分的同学 db.local.find({score:{$gt:90}}); //查找成绩大于90分$gt
查询数学成绩不等于88的同学 db.local.find({score:{$ne:88}});
查询总分大于200分的所有同学 db.local.find({score:{$gt:200}});
$lt 小于 $gt 大于 $ne 不等于
截图:
2.分页
- `db.local.find().skip(3).limit(3);`
- db.集合名称.find().跳到3.显示3条
+ limit 0,3
截图:
3.排序
db.local.find().sort({key:排序方式});
例如:
升序:
> db.local.find().sort({'score':1})
{ "_id" : ObjectId("5c1dd8b7b7c0cc1cfd4e2e59"), "contry" : "美国", "name" : "jack", "score" : 45 }
{ "_id" : ObjectId("5c1dd8b7b7c0cc1cfd4e2e5a"), "contry" : "美国", "name" : "rose", "score" : 67 }
{ "_id" : ObjectId("5c1dd8b7b7c0cc1cfd4e2e56"), "contry" : "中国", "name" : "小明", "score" : 77 }
{ "_id" : ObjectId("5c1dd8b7b7c0cc1cfd4e2e57"), "contry" : "中国", "name" : "小红", "score" : 88 }
{ "_id" : ObjectId("5c1dd8b9b7c0cc1cfd4e2e5b"), "contry" : "美国", "name" : "mick", "score" : 89 }
{ "_id" : ObjectId("5c1dd8b7b7c0cc1cfd4e2e58"), "contry" : "中国", "name" : "小张", "score" : 99 }
降序:
db.local.find().sort({'score':-1})
{ "_id" : ObjectId("5c1dd8b7b7c0cc1cfd4e2e58"), "contry" : "中国", "name" : "小张", "score" : 99 }
{ "_id" : ObjectId("5c1dd8b9b7c0cc1cfd4e2e5b"), "contry" : "美国", "name" : "mick", "score" : 89 }
{ "_id" : ObjectId("5c1dd8b7b7c0cc1cfd4e2e57"), "contry" : "中国", "name" : "小红", "score" : 88 }
{ "_id" : ObjectId("5c1dd8b7b7c0cc1cfd4e2e56"), "contry" : "中国", "name" : "小明", "score" : 77 }
{ "_id" : ObjectId("5c1dd8b7b7c0cc1cfd4e2e5a"), "contry" : "美国", "name" : "rose", "score" : 67 }
{ "_id" : ObjectId("5c1dd8b7b7c0cc1cfd4e2e59"), "contry" : "美国", "name" : "jack", "score" : 45 }
截图:
4.模糊匹配
`db.local.find({name:{$regex:'小'}});`
- `db.local.find({name:{$regex:'明'}});`