简体   繁体   中英

pagination in mongodb avoid skip() and limit()

i am new in nodejs, i want to search in big data, how can i search a document without using skip() and limit(), my database structure is

{
    "_id" : ObjectId("5a9d1836d2596d624873c84f"),
    "updatedAt" : ISODate("2018-03-05T10:13:10.248Z"),
    "createdAt" : ISODate("2018-03-05T10:13:10.248Z"),
    "phone" : "+92333333",
    "country_code" : "+92",
    "verified_user" : false,
    "verification_code" : "2951",
    "last_shared_loc_time" : ISODate("2018-03-05T10:13:10.240Z"),
    "share_loc_flag_time" : ISODate("2018-03-05T10:13:10.240Z"),
    "share_location" : true,
    "deactivate_user" : false,
    "profile_photo_url" : null,
    "__v" : 0
}

how can i search using createdAt. i need a mongodb query, which request to api and show that users

Using skip() is not recommended when we have big data in MongoDB because always requires the server to walk from the beginning of the collection, you can use _id index with limit() to do pagination, because _id field is indexed by default in MongoDB so you can use this field for a good performance.

For the first page you need to get _id value of the last document with limit() :

db.users.find().limit(8);

last_id = id;

Secondly for the next page compare this last _id value with next _id :

 db.users.find({'_id' > last_id}).limit(8);

For example assuming first _id equal to 1000 so :

                1000 < 1008  ==> PAGE 1 where last_id=1008
                1008 < 1016  ==> PAGE 2 where last_id=1016
                1016 < 1024  ==> PAGE 3 where last_id=1024
{
  uid : 1,
  name: 'abc'
  ...
} 
{
  uid : 2,
  name: 'abc'
  ...
}
....
{
  uid : 10,
  name: 'abc'
  ...
}
{
  uid : 11,
  name: 'abc'
  ...
}

Now you can query like get data where uid > 0 and uid <5

then in next slot it is get data where uid > 5 and uid <10 like wise ...

May be this approcah can help you out

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM