Skip to content

Instantly share code, notes, and snippets.

@abhiaiyer91
Created June 16, 2014 01:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abhiaiyer91/f9379f9a3370467b05dd to your computer and use it in GitHub Desktop.
Save abhiaiyer91/f9379f9a3370467b05dd to your computer and use it in GitHub Desktop.
var MongoClient = require('mongodb').MongoClient;
MongoClient.connect('mongodb://localhost:27017/weather', function(err, db) {
if(err) throw err;
db.collection('data').find().sort({"State" : 1, "Temperature" : -1}).toArray(function(err, docs) {
if(err) throw err;
var newstate = "";
var query = {};
docs.forEach(function (doc) {
console.log("Doc : " + doc);
if(newstate != doc.State) {
// update row
newstate = doc.State;
query['_id'] = doc['_id'];
var operator = { '$set' : { 'month_high' : true } };
db.collection('data').update(query, operator, function(err, updated) {
if(err) throw err;
console.dir("Successfully updated " + updated + " document!");
return db.close();
});
}
});
db.close();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment