MongoDB中怎么实现分页显示-创新互联

MongoDB中怎么实现分页显示,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

目前成都创新互联公司已为上千余家的企业提供了网站建设、域名、网页空间、绵阳服务器托管、企业网站设计、瑶海网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。

在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" : "职员"
                }
        ]
}

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注创新互联-成都网站建设公司行业资讯频道,感谢您对创新互联的支持。


网站标题:MongoDB中怎么实现分页显示-创新互联
浏览地址:http://azwzsj.com/article/cscced.html