I need to export few collections from mongo database, not all collections.
I know how to export the entire database using command line mongoexport, but I need only few collections
How do I do it?
I need to export few collections from mongo database, not all collections.
I know how to export the entire database using command line mongoexport, but I need only few collections
How do I do it?
You can export a few collections in a database. First, find the list of collections you want to export.
From mongo shell you can get list of all collections in the current database using the command: db.getCollectionNames(). You figure the collections you want to export from this list.
Suppose you want to export two collections, "movies" and "books" within the test database.
For Windows, a batch file like this will do the export. E.g.,:
mongo_exp.bat:
for %%coll IN ("movies","books") DO mongoexport --db=test --collection=%%coll --out=%%coll.json
When you run this batch file from the Windows command-prompt it will export two collections to two JSON files: movies.json and books.json.
For UNIX like OS, a bash script like this will do the export. For example: mongo_exp.sh:
#!/bin/sh
declare -a colls=("movies" "books")
for coll in ${colls[@]}
do
mongoexport --db=test --collection=$coll --out=$coll.json
done
In UNIX environment, make sure the script has execute permission before you run it.