Advertisement
Guest User

Untitled

a guest
Jul 5th, 2015
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. var MongoClient = require('mongodb').MongoClient;
  2.  
  3. MongoClient.connect('mongodb://localhost:27017/weather', function(err, db) {
  4.  
  5. if(err) {
  6. console.log(err.message);
  7. return;
  8. }
  9.  
  10. var query = {};
  11. var projection = { 'State':1, 'Temperature':1 };
  12.  
  13. var cursor = db.collection('data').find(query, projection);
  14.  
  15. // Sort by state and then by temperature (decreasing)
  16. cursor.sort({State:1,Temperature:-1});
  17.  
  18. var state = ''; // initialize to dummy value
  19. var operator = {'$set':{'month_high':true}};
  20.  
  21. cursor.each(function(err, doc) {
  22.  
  23. if (doc === null) {
  24. console.log('last record');
  25. db.close();
  26. return;
  27. }
  28.  
  29. if (err) { console.log(err.message); return; }
  30. console.log(doc);
  31.  
  32. if (doc.State !== state) {
  33. // first record for each state is the high temp one
  34. state = doc.State;
  35.  
  36. db.collection('data').update({'_id':doc._id}, operator, function(err, updated) {
  37. if (err) { console.log(err.message); return; }
  38. });
  39. }
  40. });
  41. //db.close();
  42. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement