MongoDB中方法limit和skip的使用
前言
在web开发过程中,我们肯定会遇到分页功能,如果是mysql我们主要是利用他的limit和offset语法,那么mongodb有类似的功能吗,答案是肯定的,mongodb有对应的limit和skip方法
下面话不多说了,来一起看看详细的使用方法
1.limit()方法
语法
db.collection.find().limit(NUMBER)
实例
> db.lesson.insertMany([{"name":"lua", "price":"$20.00"}, {"name":"Go", "price":"$30.00"}, {"name":"python", "price":"$40.00"}]) { "acknowledged" : true, "insertedIds" : [ ObjectId("5c78d839b881b2c3b7328602"), ObjectId("5c78d839b881b2c3b7328603"), ObjectId("5c78d839b881b2c3b7328604") ] } > db.lesson.find().limit(2) { "_id" : ObjectId("5c78d839b881b2c3b7328602"), "name" : "lua", "price" : "$20.00" } { "_id" : ObjectId("5c78d839b881b2c3b7328603"), "name" : "Go", "price" : "$30.00" }
2.skip()方法
语法
db.collection.find().skip(NUMBER)
实例
> db.lesson.find().skip(1) { "_id" : ObjectId("5c78d839b881b2c3b7328603"), "name" : "Go", "price" : "$30.00" } { "_id" : ObjectId("5c78d839b881b2c3b7328604"), "name" : "python", "price" : "$40.00" }
结合使用
> db.lesson.find().skip(1).limit(1) { "_id" : ObjectId("5c78d839b881b2c3b7328603"), "name" : "Go", "price" : "$30.00" } > db.lesson.find().limit(1).skip(1) { "_id" : ObjectId("5c78d839b881b2c3b7328603"), "name" : "Go", "price" : "$30.00" }
先后顺序没有关系
总结
相关推荐
lbyd0 2020-11-17
BigYellow 2020-11-16
sushuanglei 2020-11-12
我心似明月 2020-11-09
zhushenghan 2020-11-09
sunnnyduan 2020-10-16
不要皱眉 2020-10-14
xiaohai 2020-09-29
songxiugongwang 2020-09-22
萌亖 2020-09-17
LuckyLXG 2020-09-08
sdmzhu 2020-09-01
mkhhxxttxs 2020-09-16
xiaohai 2020-09-16
newcome 2020-09-09
jaylong 2020-08-19
大秦铁骑 2020-08-19
thatway 2020-08-19