数据排序
使用sort()
1 升序
-1 降序
自然排序 数据插入的顺序
$natural
db.stu.drop(); db.stu.insert({"name":"张三","sex":"男","age":18,"score":70,"address":"河南"}); db.stu.insert({"name":"李四","sex":"女","age":20,"score":60,"address":"山东"}); db.stu.insert({"name":"王五","sex":"男","age":17,"score":44,"address":"江苏"}); db.stu.insert({"name":"赵六","sex":"男","age":21,"score":80,"address":"山东"}); db.stu.insert({"name":"孙七","sex":"女","age":23,"score":50,"address":"湖北"}); db.stu.insert({"name":"tom","sex":"男","age":24,"score":20,"address":"海南"}); db.stu.insert({"name":"lucy","sex":"女","age":21,"score":62,"address":"浙江"}); db.stu.insert({"name":"jack","sex":"男","age":20,"score":90,"address":"美国"}); db.stu.insert({"name":"smith","sex":"男","age":19,"score":88,"address":"美国"}); db.stu.find({},{"_id":0,"address":0}); { "name" : "张三", "sex" : "男", "age" : 18, "score" : 70 } { "name" : "李四", "sex" : "女", "age" : 20, "score" : 60 } { "name" : "王五", "sex" : "男", "age" : 17, "score" : 44 } { "name" : "赵六", "sex" : "男", "age" : 21, "score" : 80 } { "name" : "孙七", "sex" : "女", "age" : 23, "score" : 50 } { "name" : "tom", "sex" : "男", "age" : 24, "score" : 20 } { "name" : "lucy", "sex" : "女", "age" : 21, "score" : 62 } { "name" : "jack", "sex" : "男", "age" : 20, "score" : 90 } { "name" : "smith", "sex" : "男", "age" : 19, "score" : 88 } //自然降序 db.stu.find({},{"_id":0,"address":0}).sort({"$natural":-1}) { "name" : "smith", "sex" : "男", "age" : 19, "score" : 88 } { "name" : "jack", "sex" : "男", "age" : 20, "score" : 90 } { "name" : "lucy", "sex" : "女", "age" : 21, "score" : 62 } { "name" : "tom", "sex" : "男", "age" : 24, "score" : 20 } { "name" : "孙七", "sex" : "女", "age" : 23, "score" : 50 } { "name" : "赵六", "sex" : "男", "age" : 21, "score" : 80 } { "name" : "王五", "sex" : "男", "age" : 17, "score" : 44 } { "name" : "李四", "sex" : "女", "age" : 20, "score" : 60 } { "name" : "张三", "sex" : "男", "age" : 18, "score" : 70 } //age升序 db.stu.find({},{"_id":0,"address":0}).sort({"age":1}); { "name" : "王五", "sex" : "男", "age" : 17, "score" : 44 } { "name" : "张三", "sex" : "男", "age" : 18, "score" : 70 } { "name" : "smith", "sex" : "男", "age" : 19, "score" : 88 } { "name" : "李四", "sex" : "女", "age" : 20, "score" : 60 } { "name" : "jack", "sex" : "男", "age" : 20, "score" : 90 } { "name" : "赵六", "sex" : "男", "age" : 21, "score" : 80 } { "name" : "lucy", "sex" : "女", "age" : 21, "score" : 62 } { "name" : "孙七", "sex" : "女", "age" : 23, "score" : 50 } { "name" : "tom", "sex" : "男", "age" : 24, "score" : 20 } //age升序,score降序 观察jack和李四 db.stu.find({},{"_id":0,"address":0}).sort({"age":1,"score":-1}); { "name" : "王五", "sex" : "男", "age" : 17, "score" : 44 } { "name" : "张三", "sex" : "男", "age" : 18, "score" : 70 } { "name" : "smith", "sex" : "男", "age" : 19, "score" : 88 } { "name" : "jack", "sex" : "男", "age" : 20, "score" : 90 } { "name" : "李四", "sex" : "女", "age" : 20, "score" : 60 } { "name" : "赵六", "sex" : "男", "age" : 21, "score" : 80 } { "name" : "lucy", "sex" : "女", "age" : 21, "score" : 62 } { "name" : "孙七", "sex" : "女", "age" : 23, "score" : 50 } { "name" : "tom", "sex" : "男", "age" : 24, "score" : 20 }
分页显示
skip(n):表示跨过多少数据行
limit(n):取出的数据行的个数限制
for(var i=0;i<100;i++){ db.data.insert({"id":i}); } db.data.find({},{"_id":0}) { "id" : 0 } { "id" : 1 } ............ { "id" : 19 } db.data.find({},{"_id":0}).skip(0).limit(5); { "id" : 0 } { "id" : 1 } { "id" : 2 } { "id" : 3 } { "id" : 4 } db.data.find({},{"_id":0}).skip(5).limit(5); { "id" : 5 } { "id" : 6 } { "id" : 7 } { "id" : 8 } { "id" : 9 }