How to get collection count of mongodb with just one cli/bash command?
I know we can get the above kind of output with the Postgresql database using the below syntax.
psql -U postgres testdb -c "select id from org where email='user11@yahoo.com';"
How can we do the same thing in MongoDB with one cli/bash command.
Tried the below/above similar command but don’t know the exact syntax
mongo testdb -c "db.user.count();"
getting below unrecognised option error.
Error parsing command line: unrecognised option '-c'
what’s the right syntax to get the desired output with just one cli/bash command?
>Solution :
To get the collection count in MongoDB using a single CLI/bash command, you can use the --eval option along with the mongo command. The syntax is slightly different from PostgreSQL. Here’s how you can do it:
mongo testdb --eval "db.collectionName.count()"
Replace testdb with your actual database name and collectionName with the name of the collection for which you want to get the count.
For example, if you have a collection named "users" in the "testdb" database, you would use:
mongo testdb --eval "db.users.count()"
This command will execute the JavaScript code "db.users.count()" using the MongoDB shell and provide you with the collection count.