Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var MongoClient = require('mongodb').MongoClient;
- MongoClient.connect('mongodb://localhost:27017/weather', function(err, db) {
- if(err) {
- console.log(err.message);
- return;
- }
- var query = {};
- var projection = { 'State':1, 'Temperature':1 };
- var cursor = db.collection('data').find(query, projection);
- // Sort by state and then by temperature (decreasing)
- cursor.sort({State:1,Temperature:-1});
- var state = ''; // initialize to dummy value
- var operator = {'$set':{'month_high':true}};
- cursor.each(function(err, doc) {
- if (doc === null) {
- console.log('last record');
- db.close();
- return;
- }
- if (err) { console.log(err.message); return; }
- console.log(doc);
- if (doc.State !== state) {
- // first record for each state is the high temp one
- state = doc.State;
- db.collection('data').update({'_id':doc._id}, operator, function(err, updated) {
- if (err) { console.log(err.message); return; }
- });
- }
- });
- //db.close();
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement