Reputation: 13
I am currently experiencing a major headache (for a middle-aged duffer, that is). My current problem is this: I have to use D3.js to visualise data stored in a local MongoDB database. I have the visualisation pretty much sorted, just one or two minors, but I think I can sort them. My problem is actually getting the data out of the database. I can import and export to my heart’s content using the shell, but I need to automate the process. The step I am having no progress with is this: Using Node.js, I can connect to my local MongoDB databse with the following script:
**
var MongoClient = require('mongodb').MongoClient, format = require('util').format;
MongoClient.connect('mongodb://127.0.0.1:27017' function(err,db){
if(err){
throw err;
}else{
console.log("Connected");
}
db.close();
});
**
Whilst I am connected, I need to export one of three collections (newYork, manchester or london) , as a .json array, from a database called monopoly. I have to use javascript and I need to export an entire collection to a folder location that I can choose, which will allow me to direct my D3.js to it I think mongoexport is the method I want as when I try this from the mongo shell, I get exactly what I want Any enlightenment would be gratefully received Thanks S
Upvotes: 0
Views: 4784
Reputation: 51
This documentation might help. https://docs.mongodb.com/manual/reference/program/mongoexport/
Try this
app.get('/export', function(req, res)
{
var spawn = require('child_process').spawn,
ls = spawn('mongoexport',['--db','monopoly','--collection','newYork']);
res.sendfile('/home/database.csv')
});
Upvotes: 4