Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get collection count of mongodb with just one cli/bash command?

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading