mongo数据排序和分页显示

Posted 梦想远航

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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数据排序和分页显示的主要内容,如果未能解决你的问题,请参考以下文章

如何对分布式数据进行排序和分页?

表单查询和分页查询和查询排序

具有自己数据库的微服务的聚集,排序,过滤和分页

问题:使用 jquery 插件 tablesorter 进行表格排序和分页

使用 gridview asp.net 进行排序和分页

JPA 排序和分页