MongoDB之資料查詢(分頁顯示)

stonebox1122發表於2017-08-24
在MongoDB裡面的資料分頁顯示也是符合大資料要求的操作函式:
skip(n):表示跨過多少資料行;
limit(n):取出的資料行的個數限制。

範例:分頁顯示(第一頁,skip(0)、limit(5))
> db.emp.find().skip(0).limit(5).sort({"age":-1}).pretty();
{
        "_id" : ObjectId("599108423268c8e84253be2c"),
        "name" : "鄭七",
        "sex" : "女",
        "age" : 50,
        "sal" : 4700,
        "loc" : "成都"
}
{
        "_id" : ObjectId("599108423268c8e84253be28"),
        "name" : "孫三",
        "sex" : "男",
        "age" : 40,
        "sal" : 2000,
        "loc" : "深圳"
}
{
        "_id" : ObjectId("599148bf0184ff511bf02b95"),
        "name" : "林E",
        "sex" : "男",
        "age" : 40,
        "sal" : 8000,
        "loc" : "北京",
        "course" : [
                "語文",
                "政治"
        ],
        "parents" : [
                {
                        "name" : "林E父親",
                        "age" : 70,
                        "job" : "工人"
                },
                {
                        "name" : "林E母親",
                        "age" : 68,
                        "job" : "局長"
                }
        ]
}
{
        "_id" : ObjectId("599108433268c8e84253be2d"),
        "name" : "王八",
        "sex" : "男",
        "age" : 35,
        "sal" : 8000,
        "loc" : "北京"
}
{
        "_id" : ObjectId("599129a00184ff511bf02b87"),
        "name" : "劉A",
        "sex" : "男",
        "age" : 35,
        "sal" : 8000,
        "loc" : "北京",
        "course" : [
                "語文",
                "數學",
                "英語",
                "音樂",
                "政治"
        ]
}

範例:分頁顯示(第二頁,skip(5)、limit(5))
> db.emp.find().skip(5).limit(5).sort({"age":-1}).pretty();
{
        "_id" : ObjectId("599129a00184ff511bf02b88"),
        "name" : "劉B",
        "sex" : "男",
        "age" : 35,
        "sal" : 8000,
        "loc" : "北京",
        "course" : [
                "語文",
                "數學"
        ]
}
{
        "_id" : ObjectId("599129a00184ff511bf02b89"),
        "name" : "劉C",
        "sex" : "男",
        "age" : 35,
        "sal" : 8000,
        "loc" : "北京",
        "course" : [
                "語文",
                "數學",
                "英語"
        ]
}
{
        "_id" : ObjectId("599129a00184ff511bf02b8a"),
        "name" : "劉D",
        "sex" : "男",
        "age" : 35,
        "sal" : 8000,
        "loc" : "北京",
        "course" : [
                "語文",
                "數學",
                "政治"
        ]
}
{
        "_id" : ObjectId("599129a20184ff511bf02b8b"),
        "name" : "劉E",
        "sex" : "男",
        "age" : 35,
        "sal" : 8000,
        "loc" : "北京",
        "course" : [
                "語文",
                "政治"
        ]
}
{
        "_id" : ObjectId("599148bd0184ff511bf02b93"),
        "name" : "林C",
        "sex" : "男",
        "age" : 35,
        "sal" : 8000,
        "loc" : "北京",
        "course" : [
                "語文",
                "數學",
                "英語"
        ],
        "parents" : [
                {
                        "name" : "林C父親",
                        "age" : 60,
                        "job" : "工人"
                },
                {
                        "name" : "林C母親",
                        "age" : 59,
                        "job" : "職員"
                }
        ]
}

這兩個分頁的控制操作,就是在以後只要是存在有大資料的資訊情況下都會使用它。

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28536251/viewspace-2144096/,如需轉載,請註明出處,否則將追究法律責任。

相關文章