MCF
MCF

Reputation: 778

MongoDB Query for pagination from Java Driver

Is the following Query the correct way to perform pagination in MongoDB from Java Driver end ?

Criteria criteria = new Criteria("users").in(userID);
Query query = new Query(criteria).skip(start).limit(count);

In this example "start" and "count" are the method parameters. Is there any performance issue in this approach ? If so, what are the alternative ways of Querying in order to address pagination in MongoDB Queries ?

Upvotes: 0

Views: 3894

Answers (1)

Trisha
Trisha

Reputation: 3931

Using skip and limit is a common pattern for getting pages of results from a database.

In terms of performance, you always need to be aware when accessing a database that there is a performance cost for the network hop between your application and the database server, assuming they're on different machines. In the case of your example code, this should only be a single call over the network, which is fine. However, be aware that if you have a very large count value that the results will come back in batches (i.e. require multiple network calls), defaulting to 101 in the first batch (see: http://docs.mongodb.org/manual/core/read-operations/#cursor-behaviors).

Upvotes: 1

Related Questions