DarkMatter in Cyberspace
  • Home
  • Categories
  • Tags
  • Archives

Update Data in MongoDB


Say you want to update data in updated.csv into "fairs" collection of "meteor" database in MongoDB.

  1. Convert data to be updated in csv file to json file: csvjson updated.csv rawdata.json;

  2. Load this file in a js script "my-script" with var data = require('./rawdata.json');;

  3. 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));
    
  4. Run my-script with node my-script;

  5. Import result.json into a new collection "tmp" of MongoDB database with mongoimport -d $TargetDB -c $TargetCol --type json --file result.json --jsonArray;

  6. Update collection fairs while traversing collection tmp;

    db = connect("localhost:27017/meteor");
    cursor = db.tmp.find();
    while ( cursor.hasNext() ) {
      db.fairs.save(cursor.next());
    }
    
  7. Save above script into update.js;

  8. Update data with mongo update.js;

Ref:

Write Scripts for the mongo Shell



Published

Mar 6, 2015

Last Updated

Mar 6, 2015

Category

Tech

Tags

  • mongodb 24
  • script 4
  • shell 46

Contact

  • Powered by Pelican. Theme: Elegant by Talha Mansoor