首页 > 代码库 > mongo数据排序和分页显示
mongo数据排序和分页显示
数据排序
使用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 }
mongo数据排序和分页显示
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。