how do I import json generated by javascript into Mongodb -
i need rid of newline character separates objects in json file can import mongodb without having objects in array. use in javascript this? need data in format can import:
{ name: "widget 1", desc: "this widget 1" }
{ name: "widget 2", desc: "this widget 2" }
the answer is, dont have convert file array, mongoimport
expects "json-line" format have.
this format performance, because don't have load @ once, instead mongo take line line. imagine billion lines, if convert array, cost memory...
this way linear time operation, lines gets streamed db.
look here:
http://zaiste.net/2012/08/importing_json_into_mongodb/
however, if think need conversion, this:
fs.readfile('my.json', function(e, text) { var arraylikestring = "[" + text.split('\n').join(',') + "]"; var array = json.parse(arraylikestring); })
to import array of objects use command:
mongoimport --db <db-name> --collection <coll-name> --type json --jsonarray --file seed.json
note option: --jsonarray
finally:
take @ npm package, looks promisingly:
Comments
Post a Comment