1

I'm using Robomongo 0.8.4 and i've 15 collections in single db. Every time manually doing export or import using CMD. For example if i need take 10 import collections means, 10 times run the mongo command in CMD. My Sample import command:

CD C:\Program Files\MongoDB\Server\3.2\bin>

mongoimport --db sampleDB --collection user1 --type json --file D:\user.json

So avoiding this one, is is possible to put all import or export commands in batch or javascript file and run that file in single execute?

How to resolve this one?

user3114967
  • 639
  • 5
  • 15
  • 38
  • You could try to write your own nodejs script, that would loop over each collection in your database and execute your export command with ShellJs – cl3m Apr 27 '17 at 14:53

3 Answers3

1

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 (/*....*/)
})
cl3m
  • 2,791
  • 19
  • 21
0

so just prepare text file with whole commands separated by line break and save it like myscript.bat, then run it.

CD "C:\Program Files\MongoDB\Server\3.2\bin>"
set list=user1 user2 user3
for %%a in (%list%) do (
   mongoimport --db sampleDB --collection %%a --type json --file D:\%%a.json
)

but expand list for as much as exports you need.

How do you loop in a Windows batch file?

Image with sample batch file

Community
  • 1
  • 1
Bukolski
  • 11
  • 3
0

I was looking for a solution around the web. I found that mongorestore may be an easier option. Maybe I am misunderstanding the context of your question but here is what I did.

mongorestore -d mongodb --dir=folderWithJsonCollections
Mark Odey
  • 170
  • 2
  • 12