How we can use $count clause in MongoDB Compass

Hi
I want to get the count of total records in a collection w.r.t to a filter so for that I typed the query in Robo 3T as
db.collectionA.find({"userId":"u123"}).count()
and it works fine for me and show the output.

But when I moved to MongoDB Compass
i was confused that where to write the count clause, can anybody help me to do this?

In Compass, when you run a query you see the number of results without the need for a count.

Alternatively, you can use the embedded shell and use the same command you use in Robo 3T.

it show N/A in total documents (1 of 20 /N/A)

Weird. Is it a big collection? Can you share a screenshot?

Yes it is. ok here is the screenshot

The size of the collection might be the reason then. It’s possible that the count times out. You can try expanding the query bar by clicking on Options. On the bottom right, you’ll be able to set maxTimeMS. By default, it’s set to 5000 but you can increase it.

1 Like

But where to write the count clause in MongoDB Compass. Can you share the screenshot, that would be great!

Hi, you don’t need to specify the “count” clause explicitly in Compass UI. Simply put filter criteria and hit enter - it would return all the values matching that criteria and would show:

Displaying documents 1-20 of 150
Assuming, there were 150 records that matched your filter criteria.

However, as suggested by @Massimiliano_Marcon - please click to expand the OPTIONS button and try increasing the MAXTIMEMS from 5000 to 10,000 or something and then see. You could even try combination of SKIP and LIMIT if the data set count returned from your query criteria is extremely very large number.

Otherwise, simply use the MongoDB shell that comes with new version of Compass UI and write the same query that you would use with Robo3T.

Thanks @ManiK for detailed message.
The thing is that we have some limitation by the client that we can only do queries via Mongo Compass & it’s version is 1.19.X. we can’t update it.
Secondly we have hundred and thousands of documents in a collection so it show N/A instead of total document count. (check this image)

The solution is that I have around 10000000+ documents in a collection and as @Massimiliano_Marcon and @ManiK said that increase the MAXTIMEMS limit so I did the same step and got the solution.

Here is this screenshot with success:

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.