Why is Codementor Io so slow

MongoDB paging

When using MongoDB, are there special patterns to e.g. B. to create a side view? Let's say a blog listing the 10 most recent posts that you can navigate backwards to older posts.

Or do you solve it with an index to blogpost.publishdate, for example, and just skip and limit the result?


Using skip + limit is not a good way to paging when performance is an issue or when you have large collections. As the number of pages increases, it gets slower and slower. When using skip, the server must iterate through all documents (or index values) from 0 to the offset value (skip).

It's much better to use a range (+ limit) query where you pass in the range value of the last page. For example, if you sort by "Date published", just pass the last value for "Date Published" as the query to get the next page of data.

  1. Area-based paging is difficult to implement when you have many ways to sort items.
  2. Remember that range-based paging is no longer possible if the field value of the sort parameter is not unique.

Possible solution: Try to simplify the design and see if we can sort by ID only or a unique value.

And if we can, area-based pageing can be used.

The usual method is to use sort (), skip (), and limit () to implement the paging described above.

This is the solution I used when my collection got too big to be returned in a single query. It takes advantage of the inherent order of the field and allows you to iterate through a collection by the specified stack size.

Here it is as npm module, mongoose paging, full code is below:

Attach it to your model as follows:

Then ask as follows:

Area-based paging is doable, but you need to be smart about how to minimize / maximize the query.

If you can afford it, you should try caching the results of a query in a temporary file or collection. Thanks to TTL collections in MongoDB, you can put your results in two collections.

  1. Search + user + parameter query (TTL whatever)
  2. Results of the query (TTL independent + cleaning interval + 1)

If you use both assertions, you will not get partial results as the TTL approaches the current time. You can use a simple counter when saving the results for a VERY simple range query at that point.

Here is an example of getting a list of documents sorted (zero based) using the official C # driver.

All sorting and paging operations are performed on the server side. While this is an example in C #, the same can be applied to other voice ports as well.

See http://docs.mongodb.org/ecosystem/tutorial/use-csharp-driver/#modifying-a-cursor-before-enumerating-it.

We use cookies and other tracking technologies to improve your browsing experience on our website, to show you personalized content and targeted ads, to analyze our website traffic, and to understand where our visitors are coming from.

By continuing, you consent to our use of cookies and other tracking technologies and affirm you're at least 16 years old or have consent from a parent or guardian.

You can read details in our Cookie policy and Privacy policy.