温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

MongoDB中怎么实现分页显示

发布时间:2021-07-16 16:54:30 来源:亿速云 阅读:165 作者:Leah 栏目:关系型数据库

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



看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI