user2702383
user2702383

Reputation: 11

Implementation of limit in mongodb

My collection name is trial and data size is 112mb

My query is,

db.trial.find()

and i have added limit up-to 10.

db.trial.find.limit(10).

but the limit is not working.the entire query is running.

Upvotes: 0

Views: 58

Answers (2)

Neil Lunn
Neil Lunn

Reputation: 151112

The .limit() modifier on it's own will only "limit" the results of the query that is processed, so that works as designed to "limit" the results returned. In a raw form though with no query you should just have the n scanned as the limit you want:

db.trial.find().limit(10)    

If your intent is to only operate on a set number of documents you can alter this with the $maxScan modifier:

db.trial.find({})._addSpecial( "$maxScan" , 11 )

Which causes the query engine to "give up" after the set number of documents have been scanned. But that should only really matter when there is something meaningful in the query.

If you are actually trying to do "paging" then you are better of using "range" queries with $gt and $lt and cousins to effectively change the range of selection that is done in your query.

Upvotes: 0

Martin Konecny
Martin Konecny

Reputation: 59611

Replace

db.trial.find.limit(10)

with

db.trial.find().limit(10)

Also you mention that the entire database is being queried? Run this

db.trial.find().limit(10).explain()

It will tell you how many documents it looked at before stopping the query (nscanned). You will see that nscanned will be 10.

Upvotes: 1

Related Questions