Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's faster: `find().limit(1)` or `findOne()` in MongoDB/Mongoose?

I've read sources that say MongoDB's findOne() is much slower than find().limit(1), but then I've also heard otherwise. What's actually the current truth?

Article from March 2013: "It is significantly faster to use find() + limit() because findOne() will always read + return the document if it exists. find() just returns a cursor (or not) and only reads the data if you iterate through the cursor."

SE answer from 2011: "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."

I hope whatever the answer is, it is also consistent with using Mongoose's find().sort().limit(1) and findOne().

like image 415
Melissa Avatar asked Oct 15 '15 19:10

Melissa


People also ask

What is the difference between find and findOne in mongoose?

The findOne() returns first document if query matches otherwise returns null. The find() method does not return null, it returns a cursor. There is no document present in the above collection.

What is findOne in mongoose?

Mongoose | findOne() Function The findOne() function is used to find one document according to the condition. If multiple documents match the condition, then it returns the first document satisfying the condition.

What does limit do in mongoose?

The limit() method in Mongoose is used to specify the number or a maximum number of documents to return from a query.

How does findOne work in MongoDB?

MongoDB findOne() method returns only one document that satisfies the criteria entered. If the criteria entered matches for more than one document, the method returns only one document according to natural ordering, which reflects the order in which the documents are stored in the database.


2 Answers

Both are equally fast.

When you do find().limit(1) no query is send to the server. You just prepare the query client side. As long as you don't retrieve any documents you can still modify the cursor, thus the query (eg by adding a sort).

So if you benchmark only the find().limit(1) you'll find it's a lot faster, because the query isn't executed. Arguably you're benchmarking useless code.

like image 59
Arnold Daniels Avatar answered Oct 16 '22 19:10

Arnold Daniels


db.collection.findOne() Vs db.collection.find().limit(1)

  • find() returns a cursor whereas findOne() returns the exact document.
  • It is faster to use find() + limit() because findOne() will always read + return the document if it exists.
  • find() just returns a cursor (or not) and only reads the data if you iterate through the cursor.
  • find() has a cursor and hence you can use explain() with your query in the mongo shell to see the winning plan and other details on the execution of your query.

Extra: limit(-1) and limit(0)

  • A limit() value of 0 (i.e. .limit(0)) is equivalent to setting no limit.
  • A negative limit is similar to a positive limit but closes the cursor after returning a single batch of results.

For more information, you can refer the official block: http://docs.mongodb.org/manual/reference/method/cursor.limit/]1

like image 40
Mayur Avatar answered Oct 16 '22 19:10

Mayur