The number of mongodb against

My problem: there is a collection of users. Im trying to find if user with _id=xxx somevalue > 5 .

I am wondering what will be faster using find(...).count() > 0 or findOne(...) != null ? Or maybe there is another, faster / better way?

+4
source share
2 answers

The difference between the request times should be almost negligible, since both of them limit the boundaries of the unique _id, so they will be found immediately. The only slight advantage here is count , because db will return int instead of the whole document. Thus, the time you save is solely due to the transfer of data from db to the client.

If you want your task to make a type request and you do not need data, use count

+3
source

Using count () does not bind a document, unlike find (), to a cache that stores your memory. Memory is your most valuable resource when using mongodb or any other database ... except for a fast disk.

+1
source

All Articles