0 votes
1 view
in Web Technology by (47.8k points)

What is the difference between, I basically wanted to find all the documents in the mycollection. db.mycollection.count() vs db.mycollection.find().count()?

They both return the same result. Is there any reason why would somebody choose the count() vs the find().count()? In contrast to the fact that find() has, a default limit applied (correct me if I'm wrong) to which you would have to type "it" in order to see more in the shell.

2 Answers

0 votes
by (107k points)

You can understand the difference between count() and find().count() by following answer db.collection.count() and cursor.count() are simply wrappers around the count command thus running db.collection.count() and cursor.count() with/without the same same will return the same query argument, will return the same result. However the count result can be inaccurate in sharded cluster. 

0 votes
by (80.9k points)

db.collection.count() without parameters counts all documents in a collection whereas db.collection.find() without parameters matches all documents in a collection, and appending count() counts them, so there is no difference.

Related questions

0 votes
2 answers
0 votes
1 answer
0 votes
2 answers
0 votes
2 answers
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...