If you have NodeJs installed, you could try a script like this:
// This script list all the collections from the database
// and execute a mongoimport command corresponding to the collection name
// npm install --save shelljs
// npm install --save mongojs
var shell = require('shelljs')
var mongojs = require('mongojs')
var db = mongojs('username:password@localhost:27017/sampleDB')
db.getCollectionNames(function (err, names) {
if (err) {
console.log(err)
process.exit(1)
}
names.forEach((name) => {
var cmd = 'mongoimport --db sampleDB --collection ' + name + ' --type json --file D:\\' + name + '.json'
console.log('executing: ' + cmd)
shell.exec(cmd, function (code, stdout, stderr) {
if (code != 0) {
console.error('Error: ' + code)
console.log('Program output:', stdout)
console.log('Program stderr:', stderr)
}
})
})
process.exit(0)
})
Note 1
you need to install the shelljs and mongjs npm packages.
Note 2
This script assumes there are already some collections in your database,
and that there is a file named after each collection in D:\
Note 3:
I have not tested the shell.exec part
Note 4
If you don't want to read the collection's names directly from the database, you could hard code the collections names directly in the script:
var collections = [{coll: "user1", file: "user"}, {coll: "admin", file: "user2"}]
collections.forEach((item) => {
var cmd = 'mongoimport --db sampleDB --collection ' + item.coll + ' --type json --file D:\\' + item.file + '.json'
shell.exec (/*....*/)
})