3

I am using MongoTemplate to execute my Mongo queries. I wanted to know if count works with limit set?

Also why find query searches full collection (according to query) although limit is set? For e.g. the query i wrote might result in having 10000 records, but i want only 100 records and for that i have set limit to 100 and then fired find query. But still query goes on to search full 10000 records.

dataQuery.limit(100);
List<logs> logResultsTemp = mongoTemplate1.find(dataQuery, logs.class);

Is their any limitations in using limit command?

Lucas
  • 14,227
  • 9
  • 74
  • 124
Jay
  • 31
  • 1
  • 1
  • 4

1 Answers1

4

Limit works fine (at least on spring data version 1.2.1 that I use). Perhaps it was a problem on your your version?

About count, there is a specific method to get your collection count, so you don't need to care about the amount of data that your system will fetch:

mongoTemplate.count(new Query(), MyCollection.class)

Btw, if you try this directly on your mongodb console: db.myCollection.limit(1).count() you will get the actual total of documents in your collection, not only one. An so it is for the mongoTemplate.count method, so:

mongoTemplate.count(new Query().limit(1), MyCollection.class)

will work the same way.

Beccari
  • 761
  • 12
  • 20