Export valid json from mongodb collection

后端 未结 2 504
终归单人心
终归单人心 2020-12-17 23:04

I am trying to export valid json from a mongodb collection I created using node and instagram\'s api. I must be missing something as it seems like it should be super simple.

相关标签:
2条回答
  • 2020-12-17 23:39

    As mentioned in the comments, parsing the result of mongodump directly probably isn't a good idea. Mongo doesn't give you any guarantee that the elements in the dump will be a valid json (and they're not).

    But if you're so inclined to do this you can use event-stream to read newline delimited stream of objects and a parser that allows your json to have single quotes instead of double quotes (i.e. hanson).

    Then your code might look something like this:

    var es = require('event-stream'),
        hanson = require('hanson'),
        in = process.stdin,
        out = process.stdout;
    
    
    in //read the input stream
        .pipe(es.split()) //split it on newline
        .pipe(es.map(function(data, cb) {
            if (data === '') { //necessary due to the last element produced by split
                cb();
            } else {
                cb(null, hanson.parse(data)); //parse the line with hanson
            }
        }))
        .pipe(es.writeArray(function (err, array){ //convert resulting objects to array
            var strArray = JSON.stringify(array);
            out.write(strArray + '\n'); //write the resulting array to output stream
        }));
    

    If you had a stream of valid json objects (with double quotes), the same code would get shortened to:

    var es = require('event-stream'),
        hanson = require('hanson'),
        in = process.stdin,
        out = process.stdout;
    
    
    in
        .pipe(es.split()) 
        .pipe(es.parse())
        .pipe(es.writeArray(function (err, array){ 
            var strArray = JSON.stringify(array);
            out.write(strArray + '\n'); 
        }));
    

    Skipped error handling for clarity.

    0 讨论(0)
  • 2020-12-17 23:49

    using themongoexport util use --jsonArray

    http://docs.mongodb.org/manual/reference/program/mongoexport/#cmdoption--jsonArray

    0 讨论(0)
提交回复
热议问题