It can be tricky to export and import collections in MongoDb. I will explain a single line command explaining in detail what each term means for both exporting and importing collections in this article.

 

Export Collection in MongoDB

sudo mongoexport --db dbName -c collectionName --out exportFileName.json

The terms used in above terminal statement

sudo : sudoer privilege
mongoexport: service we are using for exporting collection
--db: representing database, any word separated by space after this term will be taken as database name
-c: representing collection, any word separated by space after this term will be taken as collection(table) name
--out: representing output file, any word separated by space after this term will be taken as output filename

Now we have three options in above file which are explained below

dbName: Database name from which you need to export data
collectionName: Collection name from which you need to export data
exportFileName: File name to which collection is exported

 

Import Collection in MongoDB

sudo mongoimport --db dbName --collection collectionName --file importFileName.json

Ther terms used in above terminal statement

sudo: sudoer privilage
mongoimport: service we are using for importing collection
--db: representing database, any word separated by space after this term will be taken as database name
--collection: collection, any word separated by space after this term will be taken as collection(table) name
--file: input file, any word separated by space after this term ill be taken as input filename

Now we have three options in above file which are explained below

dbName: Database name from which you need to export data
collectionName: Collection name from which you need to export data
importFileName: File name from which collection is imported

 

Additional Tip:
As you have seen i have used mixed specifiers
-d can be used in place of --db
-o can be used in place of --out etc.


It all depends on your choice and for more details follow official documentation.