Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

I am working on a project and I am unsure if there is a difference between the way the find cursor works and the way the findOne cursor works. Is findOne just a wrapper for find().limit(1)? I was looking around for it and maybe someone knows if mongodb has a special method for it or not. I am working with the PHP API for mongodb if that makes a difference.

share|improve this question

2 Answers 2

up vote 9 down vote accepted

Based on my own benchmarks, find().limit(1) is orders of magnitude faster than findOne().

There is either an error in the MongoDB documentation or a bug in findOne(). findOne() performs more like find().limit(N) where N is the number of documents the query would return. I figured this out while trying to figure out why my simple queries were so slow!

update: response from a 10gen (MongoDB) engineer:

The two queries you are executing are very different. A find query returns a cursor, this is essentially a no-operation scenario, as no actual data is returned (only the cursor information). If you call findOne, then you are actually returning the data and closing the cursor. The docs should definitely be clearer :-)

Update: Indeed, if the find().limit(1) document is retrieved, the orders of magnitude speed difference seems to disappear. Also, I could not reproduce the major speed difference with the MongoDB JavaScript driver. I originally benchmarked using the MongoDB Java driver.

share|improve this answer
    
Great find. Important question, though: Do your benchmarks account for the extra operations you'd have to do with find().limit(1) in the course of normal programming (like actually retrieving the data and closing the cursor) that findOne() automatically does for you anyway? –  Nick Chammas Nov 4 '11 at 6:07
    
@Nick: I think extra operations were covered. I was finding a random document (cookbook.mongodb.org/patterns/random-attribute), getting the document with .next() and removing it from the collection. I didn't manually close any cursors... –  Leftium Nov 4 '11 at 6:41
    
@Leftium then i must ask is it faster to do a find.limit(1) and then get the cursur value or is it faster to do a findone() –  WojonsTech Nov 6 '11 at 9:30
    
@WojonsTech: a quick benchmark in JS shows findOne() is actually faster. Results may vary by driver/platform, though. For example, I could not reproduce the orders of magnitude speed difference in JS that I originally observed with the Java driver. –  Leftium Nov 7 '11 at 5:24
    
Leftium, I would edit your answer to stress that when you actually retrieve the document (which you normally would), the two functions are actually identical, just as the documentation states. Right now someone will likely read the bolded line at the beginning of your answer and conclude that if they want to retrieve one document, findOne() is worse than find().limit(1), which is incorrect. –  Nick Chammas Apr 18 '12 at 15:57

findOne() is indeed syntactic sugar for find().limit(1), given that you are actually retrieving the document (as opposed to just returning the cursor with find()).

See Leftium's answer and updates for more detail.

share|improve this answer
    
okay thank you i dont like using synimus functions in my programming would rather put limit one myself just so all my code is easy to track down. –  WojonsTech Nov 3 '11 at 17:51
    
Actually in benchmarks findOne() a bit faster than find().limit(1). –  Vladimir Apr 25 '12 at 9:21
    
@DairT'arg - If you have sources or data to back up this claim, by all means post an answer with the details! From what I've gathered so far, they should be identical as long as you are retrieving the document in both cases. –  Nick Chammas Apr 25 '12 at 15:50

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.