someusername146
someusername146

Reputation: 11

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

Upvotes: 1

Views: 5854

Answers (2)

iLyas
iLyas

Reputation: 1092

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

Upvotes: 1

Saikat Hajra
Saikat Hajra

Reputation: 680

{
  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

Upvotes: 0

Related Questions