0

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

3
  • Can you simply query the data that you want and then us fs to write the returned json to a file? Commented Feb 8, 2017 at 17:55
  • Thanks Adam, but I need to eventually query the database from the web page. Commented Feb 8, 2017 at 18:20
  • I'm really confused. If you need to query the data from the webpage, then you'll need to spin up a mongodb and get the data off your localhost. If you're looking to use mongoexport from within node, this answer might help. Or this one Commented Feb 8, 2017 at 18:31

1 Answer 1

4

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') 

    });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.