Skip to content

Instantly share code, notes, and snippets.

@nycdavid
Created June 6, 2014 14:12
Show Gist options
  • Save nycdavid/ea8a653e054d6b696e0a to your computer and use it in GitHub Desktop.
Save nycdavid/ea8a653e054d6b696e0a to your computer and use it in GitHub Desktop.
Salud: Import script for JSON foods
var fs = require('fs'),
mongoClient = require('mongodb').MongoClient,
foodsJson = JSON.parse(fs.readFileSync('./foods-2011-10-03.json'));
mongoClient.connect('mongodb://127.0.0.1:27017/salud', function(err, db) {
if (err) throw err;
var collection = db.collection('foods-orig');
foodsJson.forEach(function(el, i) {
el.nutrients.forEach(function(el2, i) {
delete el2.group
});
collection.insert({
name: el.name,
group: el.group,
nutrients: el.nutrients
}, {}, function(err, foods) {
if (err) throw err;
console.log(foods[0].name + ': INSERTED');
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment