Last active
July 26, 2016 22:12
-
-
Save zanedeg/8475f7eb52577d4ffddc313cf55542ef to your computer and use it in GitHub Desktop.
Dump json file of query from mongo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// npm install mongodb | |
// node json_dump.js | |
var fs = require('fs'); | |
var mongo = require('mongodb').MongoClient; | |
var ObjectID = require('mongodb').ObjectID | |
var config = { | |
user: "", | |
password: "", | |
host: "", | |
port: , | |
name: "" | |
} | |
var filename = 'dump' | |
mongo.connect('mongodb://' + config.user + ':' + config.password + '@' + config.host + ':' + config.port + '/' + config.name, function(err, db) { | |
if (err) throw new Error(err); | |
if (!err && db) console.log('Opened connection to ' + config.name); | |
function find(collection, query, cb) { | |
db.collection(collection).find(query).toArray(cb) | |
} | |
var collection = ''; | |
var query = {}; | |
return find(collection, query, function(err, documents) { | |
var export_docs = JSON.stringify(documents, null, 2) + '\n'; | |
fs.writeFileSync('./' + filename + '.json', export_docs); | |
console.log('Finished writing ' + filename + '.json'); | |
db.close(); | |
console.log('Closed connection to ' + config.name); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment