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.
- 14,670
- 17
- 75
- 121
- 652
- 1
- 5
- 11
4 Answers
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.
- 769
- 1
- 7
- 13
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.
- 14,670
- 17
- 75
- 121
-
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 03 '11 at 17:51
-
1
-
@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
The source code may helps a lot.
It's java but I guess it can help, too.
The findOne(),
DBObject findOne(DBObject o, DBObject fields, DBObject orderBy, ReadPreference readPref,
long maxTime, TimeUnit maxTimeUnit) {
QueryOpBuilder queryOpBuilder = new QueryOpBuilder().addQuery(o).addOrderBy(orderBy)
.addMaxTimeMS(MILLISECONDS.convert(maxTime, maxTimeUnit));
if (getDB().getMongo().isMongosConnection()) {
queryOpBuilder.addReadPreference(readPref);
}
Iterator<DBObject> i = find(queryOpBuilder.get(), fields, 0, -1, 0, getOptions(), readPref, getDecoder());
DBObject obj = (i.hasNext() ? i.next() : null);
if ( obj != null && ( fields != null && fields.keySet().size() > 0 ) ){
obj.markAsPartialObject();
}
return obj;
}
And here is find()
public DBCursor find( DBObject ref ){
return new DBCursor( this, ref, null, getReadPreference());
}
As we can see that findOne() calls find() in it self, gets all the DBOject in i and then return the first.
- 3,934
- 1
- 12
- 6
You must check this link ...
http://mongoosejs.com/docs/2.7.x/docs/finding-documents.html
- find - option return array []
- findOne - option return document {}
find().limit(1)in the course of normal programming (like actually retrieving the data and closing the cursor) thatfindOne()automatically does for you anyway? – Nick Chammas Nov 04 '11 at 06:07findOne()is worse thanfind().limit(1), which is incorrect. – Nick Chammas Apr 18 '12 at 15:57Model.find(q).limit(1)VSModel.findOne(q)) – chamberlainpi Aug 25 '17 at 12:42