![]() Apart from its structure there is one very interesting property of ObjectId which is - ObjectId has natural ordering a 3-byte counter, starting with a random value.Įven I didn’t until I read the documentation.a 4-byte value representing the seconds since the Unix epoch,.I bet you didn’t know that a Mongodb ObjectId is a 12 byte structure containing This approach will make effective use of default index on _id and nature of ObjectId. """returns a set of documents belonging to page number `page_num`Ĭursor = db.find().skip(skips).limit(page_size) I am fond of Python and hence here is a small trivial function to implement pagination: def skiplimit(page_size, page_num): Through continuous skip and limit we get pagination in MongoDB. find() will return a cursor pointing to all documents of the collection and then for each page we skip some and consume some. In Mongo Shell your pagination code looks something like this // Thus combination of two naturally paginates the response. Skip(n) will skip n documents from the cursor while limit(n) will cap the number of documents to be returned from the cursor. MongoDB cursor has two methods that makes paging easy they are Hence pagination is generally handled at database level and databases are optimized for such needs too.īelow I shall explain you the 2 approaches through which you can easily paginate your MongoDB responses. Paginating on client and server side are both really very expensive and should not be considered. Showing huge set of results on user interface.So when do you paginate the response? The answer is pretty neat you paginate whenever you want to process result in chunks. MongoDB is a document based data store and hence pagination is one of the most common use case of it.
0 Comments
Leave a Reply. |