Reputation: 618
My problem is: There is a collection of users. Im trying to find, does user with _id=xxx
has somevalue > 5
.
I'm wondering, what will be faster, using find(...).count() > 0
or findOne(...) != null
? Or maybe there is some other, faster/better way?
Upvotes: 6
Views: 4535
Reputation: 11
Using count() does not pin the document, in contrary to find(), to the cache, which saves you memory. Memory is your most valuable resource, when using mongodb or any other DB... besides a fast disk.
Upvotes: 1
Reputation: 92569
The difference between the query times should be almost negligible, because they both limit the bounds of the unique _id, so they will be found immediately. The only slight edge here goes to the count
because the db will return a int instead of an entire document. So the time you save is purely because of the transfer of the data from db to client.
That being said, if your goal is to do an exists query and you don't care about the data, use the count
Upvotes: 4