• 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 }
  • 相关阅读:
    最近遇到的若干Web前端问题:disable和readonly,JqueryEasyUI,KindEditor
    最近遇到的若干Web前端问题:disable和readonly,JqueryEasyUI,KindEditor
    2015年工作中遇到的问题111-120
    2015年工作中遇到的问题111-120
    POJ 1459 Power Network
    POJ 2135 Farm Tour
    HDU 3062 Party
    学渣乱搞系列之网络流学习
    POJ 1094 Sorting It All Out
    HDU 1704 Rank
  • 原文地址:https://www.cnblogs.com/HKUI/p/6942519.html
Copyright © 2020-2023  润新知