Say you want to update data in updated.csv into "fairs" collection of "meteor" database in MongoDB.
-
Convert data to be updated in csv file to json file:
csvjson updated.csv rawdata.json
; -
Load this file in a js script "my-script" with
var data = require('./rawdata.json');
; -
Check and convert data content in my-script, and write the reulst into another json file "result.json":
var fs = require('fs'); fs.writeFile('result.json', JSON.stringify(result));
-
Run my-script with
node my-script
; -
Import result.json into a new collection "tmp" of MongoDB database with
mongoimport -d $TargetDB -c $TargetCol --type json --file result.json --jsonArray
; -
Update collection fairs while traversing collection tmp;
db = connect("localhost:27017/meteor"); cursor = db.tmp.find(); while ( cursor.hasNext() ) { db.fairs.save(cursor.next()); }
-
Save above script into update.js;
-
Update data with
mongo update.js
;
Ref:
Write Scripts for the mongo Shell