findOne vs findById

2,046 views
Skip to first unread message

Benjamin Bengfort

unread,
Jun 13, 2014, 9:25:49 AM6/13/14
to mongoo...@googlegroups.com
Perhaps I'm just bad at Googling, but I was wondering about the relative performance of findOne vs findById - or if there was no difference at all.

I'm routinely confronted with the following choice (as I'm sure many of us are):

findOne({_id: "myid"})
findById("myid")

As well as the related find*AndUpdate or find*AndRemove. 

Should I be using one over the other, or is it just syntactic sugar/convenience method? 

Best,
Ben

Jason Crawford

unread,
Jun 13, 2014, 11:55:09 AM6/13/14
to mongoo...@googlegroups.com
I'm pretty sure it's just a convenience method, since MongoDB itself doesn't have separate queries for these things. To be totally sure, you could turn on logging in Mongoose to see exactly the queries it's doing—you'll probably find that it does the same query in both cases.

Hope that helps,
Jason

--
Blog: http://blog.jasoncrawford.org  |  Twitter: @jasoncrawford




--
Documentation - http://mongoosejs.com/
Plugins - http://plugins.mongoosejs.com/
Bug Reports - http://github.com/learnboost/mongoose
Production Examples - http://mongoosejs.tumblr.com/
StackOverflow - http://stackoverflow.com/questions/tagged/mongoose
Google Groups - https://groups.google.com/forum/?fromgroups#!forum/mongoose-orm
Twitter - https://twitter.com/mongoosejs
IRC - #mongoosejs
---
You received this message because you are subscribed to the Google Groups "Mongoose Node.JS ODM" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongoose-orm...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ryan Wheale

unread,
Jun 13, 2014, 7:53:37 PM6/13/14
to mongoo...@googlegroups.com
I can verify that findById( [id] ) is just a shortcut to findOne( {_id: [id]} );  The only performance hit is an extra function call.

~Ryan
Reply all
Reply to author
Forward
0 new messages