Mongodb – How to use mongoexport in count

exportmongodb

I want to get the count of all rows in a single collection into an output file. What is the correct way of doing this

Here is what I did but I am encountering an error.

mongoexport --host 10.1.0.1:27017 --db proddb --collection  UserColl --query "{db.getCollection('UserColl').count()}" --type=csv --out=output.csv

Best Answer

I created a sample.js file like the sample below:

db=db.getSiblingDB('DbName');

printjson( db.getCollection('CollectionName').find().count() );

then I run this script below on cmd:

mongo --host IP:PORT --username username --password password --authenticationDatabase admin --quiet sample.js > sample.csv
Related Question