Prabjot Singh
Prabjot Singh

Reputation: 4767

Spring data mongodb: How to define cursor limit in findAll method

I am working with spring data mongodb geospatial criteria, my requirement is that i have to find users using item's coordinates and radius. When admin will add item,it will work on users which fulfills criteria. So for now,i am using mongotemplate's findAll method. but in future,users may be upto 50000,so i think findAll is not efficient for it. i am looking for limit set in findAll or any alternate method to do it in spring data mongodb.

My query is

List<User> users = mongoTemplate.find(
                new Query(Criteria.where("address.coordinates").within(
                        new Circle(longitude, latitude, radius))), User.class);

Please suggest me how can i use cursor limit in findall method.

I am trying to do using

DBCollection collection = mongo.getDB(envConfiguration.getDBName())
                .getCollection("Users");
        DBCursor cursor = collection.find();
        cursor.limit(1000);
        while (cursor.hasNext()) {
            System.out.println(cursor.next());
        }

But now problem is that how i will use geospatial query in this which i mentioned in example.

Thannks

Upvotes: 3

Views: 4386

Answers (1)

Ali Dehghani
Ali Dehghani

Reputation: 48133

You can use skip and limit methods offered by Query, like this:

List<User> users = mongoTemplate
                .find(new Query(Criteria.where("address.coordinates")
                        .within(new Circle(longitude, latitude, radius)))
                        .limit(1000), 
                        User.class);

You can also use a with(Pageable pageable) method and last but not least you can use Mongo Repositories and their findAll(Pageable pageable) method.

Upvotes: 3

Related Questions