The solution HW 2.2 M101JS: MongoDB for Node.js Developers

Write a program that finds the document with the highest recorded temperature for each state, and adds a “month_high” field for that document, setting its value to true. Use the weather dataset that you imported in HW 2.1.

From the top of this page, there was one additional program that should have been downloaded:mongoProc.

With it, you can test your code and look at the Feedback section. When it says “Correct amount of data documents.”, “Correct amount of month_high documents.” and “Correct month_high documents.”, you can Turn in your assignment.

Solution:

  • Create app.js
  • Paste the following code
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.Statdbe;
                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();
    });
});
  • Run with nodejs as node app.js
  • Check the result in mongo shell with query as db.data.find({“month_high”:true}); and result will be following:
{ "_id" : ObjectId("5627f4216f0eb671e142efbd"), "Day" : 23, "Time" : 1654, "State" : "Vermont", "Airport" : "BTV", "Temperature" : 57, "Humidity" : 78, "Wind Sp
eed" : 13, "Wind Direction" : 170, "Station Pressure" : 29.1, "Sea Level Pressure" : 982, "month_high" : true }
{ "_id" : ObjectId("5627f4216f0eb671e142f180"), "Day" : 11, "Time" : 1150, "State" : "California", "Airport" : "LAX", "Temperature" : 81, "Humidity" : 32, "Wind
 Speed" : 5, "Wind Direction" : 50, "Station Pressure" : 29.63, "Sea Level Pressure" : 151, "month_high" : true }
{ "_id" : ObjectId("5627f4216f0eb671e142f41d"), "Day" : 8, "Time" : 1453, "State" : "Florida", "Airport" : "ORL", "Temperature" : 83, "Humidity" : 55, "Wind Spe
ed" : 5, "Wind Direction" : 190, "Station Pressure" : 29.97, "Sea Level Pressure" : 195, "month_high" : true }
{ "_id" : ObjectId("5627f4226f0eb671e142f746"), "Day" : 11, "Time" : 1453, "State" : "New Mexico", "Airport" : "SAF", "Temperature" : 57, "Humidity" : 32, "Wind
 Speed" : 11, "Wind Direction" : 340, "Station Pressure" : 23.87, "Sea Level Pressure" : 174, "month_high" : true }
  • Then I run mongoProc and click “Test” button which shows data are fine then I click “Turn In” which shows the following screenshot.
m101js-homework_2.2

Let me know if get any problem so that we can solve each other help. As well as if you got any easy way to do it then please let me know.

Previous articleThe solution to Homework 6.5 M101JS: MongoDB for Node.js Developers
Next articleThe solution Homework 2.3 M101JS: MongoDB for Node.js Developers
Author of three Opencart book. The recent Opencart 4 book is at https://amzn.to/4dOlbOR

LEAVE A REPLY

Please enter your comment!
Please enter your name here