spawngo
A wrapper to spawn mongoimport/export processes
usage
const Spawngo = require('spawngo')
let spawngo = new Spawngo({
user: 'foo',
pw: 'bar',
'collection': 'bang'
})
let childProcess = spawngo.import('my.json')
childProcess.stdout.on('data', function (data) {
})
childProcess.stderr.on('data', function (data) {
})
childProcess.on('close', function (data) {
})
Note that mongoDb sends ALL status updates to stderr, so to better determine if your process was successful, you should ensure the status code from theclose event returned a 0, and not rely on stderr.
Exporting is nearly identical to the above, but instead of import(), you would call export()
let childProcess = spawngo.export('collectionName')
api
Constructor(options)
options (Object): Configuration object. Allowed properties are as follows (displayed with default values)
- host: 'localhost'
- user: ''
- pwd: ''
- db: ''
- collection: ''
- jsonArray:
true
- upsertFields:
undefined
- cpus: [default is number of machine's cpu cores]
- drop:
false
Instead of passing options to constructor, you can also apply your settings by using the .set() function.
If no user and password are set, then the call to mongoimport will not use authentication.
import(fileName)
fileName (String): Path of the json/csv/tsv file to import
returns: ChildProcess of the spawned query.
export(collection)
collection (String): Name of the collection to export. The exported json file will be named after the collection (i.e collectionName.json).
returns: ChildProcess of the spawned query.
set(optsObj or key, value)
optsObj (Object): Configuration object. See Constructor above to see all available options.
key (String), value (String): to update a single congiguration property, you may pass a key/value pair of strings as an argument: spawngo.set('collection', 'myThings').
returns: The spawngo instance.