码迷,mamicode.com
首页 > 编程语言 > 详细

mongo数据排序和分页显示

时间:2017-06-05 10:36:12      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:add   显示   ++   code   div   insert   var   使用   drop   

数据排序
使用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数据排序和分页显示

标签:add   显示   ++   code   div   insert   var   使用   drop   

原文地址:http://www.cnblogs.com/HKUI/p/6942519.html

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