• find, find cursors, find lt & gt, find projection, find And Modify, findOne
  • import JSON, insert Array, insert Doc
  • Regex selector
  • remove
  • save
  • skip Limit Sort
  • update, update Single, update Multi
  • upsert

find

var MongoClient = require('mongodb').MongoClient;
 
MongoClient.connect('mongodb://localhost:27017/course', function(err, db) {
    if(err) throw err;
 
    var query = { 'grade' : 100 };
 
    db.collection('grades').find(query).toArray(function(err, docs) {
        if(err) throw err;
 
        console.dir(docs);
 
        db.close();
    });
});

find cursors

var MongoClient = require('mongodb').MongoClient;
 
MongoClient.connect('mongodb://localhost:27017/course', function(err, db) {
    if(err) throw err;
 
    var query = { 'grade' : 100 };
 
    var cursor = db.collection('grades').find(query);
 
    cursor.each(function(err, doc) {
        if(err) throw err;
 
        if(doc == null) {
            return db.close();
        }
 
        console.dir(doc.student + " got a good grade!");
    });
});

find lt and gt

var MongoClient = require('mongodb').MongoClient;
 
MongoClient.connect('mongodb://localhost:27017/course', function(err, db) {
    if(err) throw err;
 
    var query = { 'student' : 'Joe', 'grade' : { '$gt' : 80, '$lt' : 95 } };
 
    db.collection('grades').find(query).each(function(err, doc) {
        if(err) throw err;
 
        if(doc == null) {
            return db.close();
        }
 
        console.dir(doc);
    });
});

find projection

var MongoClient = require('mongodb').MongoClient;
 
MongoClient.connect('mongodb://localhost:27017/course', function(err, db) {
    if(err) throw err;
 
    var query = { 'grade' : 100 };
 
    var projection = { 'student' : 1, '_id' : 0 };
 
    db.collection('grades').find(query, projection).toArray(function(err, docs) {
        if(err) throw err;
 
        docs.forEach(function (doc) {
            console.dir(doc);
            console.dir(doc.student + " got a good grade!");
        });
 
        db.close();
    });
});

findAndModify

var MongoClient = require('mongodb').MongoClient;
 
MongoClient.connect('mongodb://localhost:27017/course', function(err, db) {
    if(err) throw err;
 
    var query = { 'name' : 'comments' };
    var sort = [];
    var operator = { '$inc' : { 'counter' : 1 } };
    var options = { 'new' : true };
 
    db.collection('counters').findAndModify(query, sort, operator, options, function(err, doc) {
        if(err) throw err;
 
        if (!doc) {
            console.log("No counter found for comments.");
        }
        else {
            console.log("Number of comments: " + doc.counter);
        }
 
        return db.close();
    });
});

findOne

var MongoClient = require('mongodb').MongoClient;
 
MongoClient.connect('mongodb://localhost:27017/course', function(err, db) {
    if(err) throw err;
 
    var query = { 'grade' : 100 };
 
    function callback(err, doc) {
        if(err) throw err;
 
        console.dir(doc);
 
        db.close();
    }
 
    /* START STUDENT CODE */
    db.collection('grades').findOne(query, callback);
    /* END STUDENT CODE */
});

import JSON

var MongoClient = require('mongodb').MongoClient
  , request = require('request');
 
MongoClient.connect('mongodb://localhost:27017/course', function(err, db) {
    if(err) throw err;
 
    request('http://www.reddit.com/r/technology/.json', function (error, response, body) {
        if (!error && response.statusCode == 200) {
            var obj = JSON.parse(body);
 
            var stories = obj.data.children.map(function (story) { return story.data; });
 
            db.collection('reddit').insert(stories, function (err, data) {
                    if(err) throw err;
 
                    console.dir(data);
 
                    db.close();
            });
        }
    });
});

insert Array

var MongoClient = require('mongodb').MongoClient;
 
MongoClient.connect('mongodb://localhost:27017/course', function(err, db) {
    if(err) throw err;
 
    var docs = [ { 'student' : 'Calvin', 'age' : 6 },
                 { 'student' : 'Susie', 'age' : 7 } ];
 
    db.collection('students').insert(docs, function(err, inserted) {
        if(err) throw err;
 
        console.dir("Successfully inserted: " + JSON.stringify(inserted));
 
        return db.close();
    });
});

insert Doc

var MongoClient = require('mongodb').MongoClient;
 
MongoClient.connect('mongodb://localhost:27017/course', function(err, db) {
    if(err) throw err;
 
    var doc = { '_id' : 'calvin', 'age' : 6 };
 
    db.collection('students').insert(doc, function(err, inserted) {
        if(err) throw err;
 
        console.dir("Successfully inserted: " + JSON.stringify(inserted));
 
        return db.close();
    });
});

regex Selector

var MongoClient = require('mongodb').MongoClient;
 
MongoClient.connect('mongodb://localhost:27017/course', function(err, db) {
    if(err) throw err;
 
    var query = { 'title' : { '$regex' : 'Microsoft' } };
 
    var projection = { 'title' : 1, '_id' : 0 };
 
    db.collection('reddit').find(query, projection).each(function(err, doc) {
        if(err) throw err;
 
        if(doc == null) {
            return db.close();
        }
 
        console.dir(doc.title);
    });
});

remove

var MongoClient = require('mongodb').MongoClient;
 
MongoClient.connect('mongodb://localhost:27017/course', function(err, db) {
    if(err) throw err;
 
    var query = { 'assignment' : 'hw3' };
 
    db.collection('grades').remove(query, function(err, removed) {
        if(err) throw err;
 
        console.dir("Successfully updated " + removed + " documents!");
 
        return db.close();
    });
});

save

var MongoClient = require('mongodb').MongoClient;
 
MongoClient.connect('mongodb://localhost:27017/course', function(err, db) {
    if(err) throw err;
 
    var query = { 'assignment' : 'hw2' };
 
    db.collection('grades').findOne(query, function(err, doc) {
        if(err) throw err;
 
        doc['date_returned'] = new Date();
 
        db.collection('grades').save(doc, function(err, saved) {
            if(err) throw err;
 
            console.dir("Successfully saved " + saved + " document!");
 
            return db.close();
        });
    });
});

skip Limit Sort

var MongoClient = require('mongodb').MongoClient;
 
MongoClient.connect('mongodb://localhost:27017/course', function(err, db) {
    if(err) throw err;
 
    var grades = db.collection('grades');
 
    var cursor = grades.find({});
    cursor.skip(1);
    cursor.limit(4);
    cursor.sort('grade', 1);
    //cursor.sort([['grade', 1], ['student', -1]]);
 
    //var options = { 'skip' : 1,
    //                'limit' : 4,
    //                'sort' : [['grade', 1], ['student', -1]] };
    //var cursor = grades.find({}, {}, options);
 
    cursor.each(function(err, doc) {
        if(err) throw err;
        if(doc == null) {
            return db.close();
        }
        console.dir(doc);
    });
});

update

var MongoClient = require('mongodb').MongoClient;
 
MongoClient.connect('mongodb://localhost:27017/course', function(err, db) {
    if(err) throw err;
 
    var query = { 'assignment' : 'hw1' };
    var operator = { 'assignment' : 'hw2', '$set' : { 'date_graded' : new Date() } };
 
    db.collection('grades').update(query, operator, function(err, updated) {
        if(err) throw err;
 
        console.dir("Successfully updated " + updated + " document!");
 
        return db.close();
    });
});

update Single

var MongoClient = require('mongodb').MongoClient;
 
MongoClient.connect('mongodb://localhost:27017/course', function(err, db) {
    if(err) throw err;
 
    var query = { 'assignment' : 'hw1' };
 
    db.collection('grades').findOne(query, function(err, doc) {
        if(err) throw err;
        if(!doc) {
            console.log('No documents for assignment ' + query.assignment + ' found!');
            return db.close();
        }
 
        query['_id'] = doc['_id'];
        doc['date_returned'] = new Date();
 
        db.collection('grades').update(query, doc, function(err, updated) {
            if(err) throw err;
 
            console.dir("Successfully updated " + updated + " document!");
 
            return db.close();
        });
    });
});

update Multi

var MongoClient = require('mongodb').MongoClient;
 
MongoClient.connect('mongodb://localhost:27017/course', function(err, db) {
    if(err) throw err;
 
    var query = { };
    var operator = { '$unset' : { 'date_returned' : '' } };
    var options = { 'multi' : true };
 
    db.collection('grades').update(query, operator, options, function(err, updated) {
        if(err) throw err;
 
        console.dir("Successfully updated " + updated + " documents!");
 
        return db.close();
    });
});

upsert

var MongoClient = require('mongodb').MongoClient;
 
MongoClient.connect('mongodb://localhost:27017/course', function(err, db) {
    if(err) throw err;
 
    var query = { 'student' : 'Frank', 'assignment' : 'hw1' };
    //var operator = { 'student' : 'Frank', 'assignment' : 'hw1', 'grade' : 100 };
    var operator = { '$set' : { 'date_returned' : new Date(), 'grade' : 100 } };
    var options = { 'upsert' : true };
 
    db.collection('grades').update(query, operator, options, function(err, upserted) {
        if(err) throw err;
 
        console.dir("Successfully upserted " + upserted + " document!");
 
        return db.close();
    });
});

Homework 2.1

In this problem, you will be using an old weather dataset. You should have included with your homework files a "weather_data.csv" file. This is a comma separated value file that you can import into MongoDB as follows: mongoimport --type csv --headerline weather_data.csv -d weather -c data

You can verify that you've imported the data correctly by running the following commands in the mongo shell:

use weather db.data.find().count() 2963

Reading clockwise from true north, the wind direction is measured by degrees around the compass up to 360 degrees. 90 is East 180 is South 270 is West 360 is North

Your assignment is to figure out the "State" that recorded the lowest "Temperature" when the wind was coming from the west ("Wind Direction" between 180 and 360). Please enter the name of the state that meets this requirement. Do not include the surrounding quotes in providing your answer.

Query: db.data.find({ "Wind Direction" : { "$gt" : 180, "$lt" : 360 } }}.sort({ "Temperature" : 1}).limit(1).pretty()

Homework: Homework 2.2

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.

This is a sample document from the weather data collection:

use weather switched to db weather db.data.findOne() { "_id" : ObjectId("520bea012ab230549e749cff"), "Day" : 1, "Time" : 54, "State" : "Vermont", "Airport" : "BTV", "Temperature" : 39, "Humidity" : 57, "Wind Speed" : 6, "Wind Direction" : 170, "Station Pressure" : 29.6, "Sea Level Pressure" : 150 }

Assuming this document has the highest "Temperature" for the "State" of "Vermont" in our collection, the document should look like this after you run your program:

db.data.findOne({ "_id" : ObjectId("520bea012ab230549e749cff") }) { "_id" : ObjectId("520bea012ab230549e749cff"), "Day" : 1, "Time" : 54, "State" : "Vermont", "Airport" : "BTV", "Temperature" : 39, "Humidity" : 57, "Wind Speed" : 6, "Wind Direction" : 170, "Station Pressure" : 29.6, "Sea Level Pressure" : 150, "month_high" : true }

Note that this is only an example and not the actual document that you would be updating. Note also that our collection only has one month of data for each "State", which is why we are asking you to set "month_high".

Hint: If you select all the weather documents, you can sort first by state, then by temperature. Then you can iterate through the documents and know that whenever the state changes you have reached the highest temperature for that state.

If you got it right, it will provide a validation code for you to enter into the box below. Enter just the code, no spaces. If you fail validation or have messed up your collection beyond repair, run the following to drop the weather database so you can reimport the data and try again:

highTemp.js

var client = require('mongodb').MongoClient;
 
client.connect('mongodb://localhost:27017/weather', function(err, db) {
    if (err) throw err;
 
    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 (err) throw err;
 
        if (doc == null) {
            return db.close();
        } else 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) throw err;
            });
        }
    });
});

Homework: Homework 2.3

Blog User Sign-up and Login

If you have not already done so download the homework files for this week.

You should see four files at the highest level: app.js, users.js, posts.js and sessions.js. There is also a views directory which contains the templates for the project and a routes directory which contains our express routes.

If everything is working properly, you should be able to start the blog by typing:

npm install node app.js

Note that this requires Node.js to be correctly installed on your computer. After you run the blog, you should see the message:

Express server listening on port 3000

If you goto http://localhost:3000 you should see the front page of the blog. Here are some URLs that must work when you are done.

http://localhost:3000/signup http://localhost:3000/login http://localhost:3000/logout

When you login or sign-up, the blog will redirect to http://localhost:3000/welcome and that must work properly, welcoming the user by username.

We have removed parts of the code that uses the Node.js driver to query MongoDB from users.js and marked the area where you need to work with "TODO: hw2.3". You should not need to touch any other code. The database calls that you are going to add will add a new user upon sign-up and validate a login by retrieving the right user document.

The blog stores its data in the blog database in two collections, users and sessions. Here are two example docs for a username ‘sverch’ with password ‘salty’. You can insert these if you like, but you don’t need to.

use blog switched to db blog db.users.find() { "_id" : "sverch", "password" : "$2a$10$wl4bNB/5CqwWx4bB66PoQ.lmYvxUHigM1ehljyWQBupen3uCcldoW" } db.sessions.find() { "username" : "sverch", "_id" : "8d25917b27e4dc170d32491c6247aabba7598533" }

Once you have the the project working, the following steps should work:

go to http://localhost:3000/signup
create a user

It should redirect you to the welcome page and say: welcome username, where username is the user you signed up with. Now:

Goto http://localhost:3000/logout
Now login http://localhost:3000/login

Ok, now it’s time to validate you got it all working.

users.js

var bcrypt = require('bcrypt-nodejs');
 
/* The UsersDAO must be constructed with a connected database object */
function UsersDAO(db) {
    "use strict";
 
    /* If this constructor is called without the "new" operator, "this" points
     * to the global object. Log a warning and call it correctly. */
    if (false === (this instanceof UsersDAO)) {
        console.log('Warning: UsersDAO constructor called without "new" operator');
        return new UsersDAO(db);
    }
 
    var users = db.collection("users");
 
    this.addUser = function(username, password, email, callback) {
        "use strict";
 
        // Generate password hash
        var salt = bcrypt.genSaltSync();
        var password_hash = bcrypt.hashSync(password, salt);
 
        // Create user document
        var user = {'_id': username, 'password': password_hash};
 
        // Add email if set
        if (email != "") {
            user['email'] = email;
        }
 
          users.insert(user, function(err, inserted) {
                if (err) {
                    callback(err, null);
            } else {
                callback(null, inserted[0]);
            }
          });
    }
 
    this.validateLogin = function(username, password, callback) {
        "use strict";
 
        // Callback to pass to MongoDB that validates a user document
        function validateUserDoc(err, user) {
            "use strict";
 
            if (err) return callback(err, null);
 
            if (user) {
                if (bcrypt.compareSync(password, user.password)) {
                    callback(null, user);
                }
                else {
                    var invalid_password_error = new Error("Invalid password");
                    // Set an extra field so we can distinguish this from a db error
                    invalid_password_error.invalid_password = true;
                    callback(invalid_password_error, null);
                }
            }
            else {
                var no_such_user_error = new Error("User: " + user + " does not exist");
                // Set an extra field so we can distinguish this from a db error
                no_such_user_error.no_such_user = true;
                callback(no_such_user_error, null);
            }
        }
 
        users.findOne({'_id':username}, function(err, user) {
              callback(err,user);
          });
    }
}
 
module.exports.UsersDAO = UsersDAO;

12 comments

#214
uggs cheap says:
Saturday, October 4, 2014 2:23 PM
He additional statements to have cultivated the actual first mess on guys.
#213
Saturday, October 4, 2014 1:46 PM
dojo related to kansas city, Melissa, Prestonwood audra, tc Addison,
#212
Saturday, October 4, 2014 1:31 PM
It had not been as if the market encountered disturbing problems whom had on to it really is FCF, i really am not too concerned with its P/FCF amount: focusing on for the 5 year usual involving quite a few has been in existence 20.
#211
uggs outlet says:
Saturday, October 4, 2014 1:18 PM
Adolf furthermore Rudolf Dassler were originally in german friends exactly who good a love for shoemaking, consequently all the people mutual title your Dassler siblings shoe plant.
#209
cheap uggs says:
Saturday, October 4, 2014 11:37 AM
such a serving capabilities hassle,unchanged inexpensive computer memory means that allow you to have the unavoidable gadgets in your finger tips to become intent on your golf game.
#205
Saturday, October 4, 2014 5:42 AM
and especially in air pressure fragile, there's not so much unpredictability.
#158
Tuesday, September 30, 2014 6:07 AM
this business deal and the cheapest selection to receive Durant could be indicator for still under shield.the particular sales that will Durant built starting from his KDs only real very increased this type of year, them progression will maintain as the golfer becomes better height.
#126
uggs cheap says:
Friday, September 26, 2014 4:29 PM
He should probably modernize FIFA's 209 member countries on your practice friday at an annual the nation's lawmakers during Sao Paulo.sponsors barely comment on FIFA hardships, And Adidas is most likely the lengthiest standing upright rest of the world goblet other half.FIFA campaign overseer Thierry Weil stated that there seems to be prolonged connection with large girlfriend,each of vendors haven't requested for anything which is not taught in ongoing basic research by the life values panel, Weil being said in your firm stand out.best rate FIFA holds pay for over additional than $100 million all the much more four prolonged that it is linked to the World pin.Adidas registered with FIFA with regard to that 1970 sector glass in addition to the the other day reconditioned for four a great deal competitions throughout 2030,Adidas has got a long term but flourishing cooperation because of FIFA that we are eager for continue, all of the stiff cited.credit expand it is really entire world panes support from economy is shown from the 2018 wedding russian federation and as a result 2022,Our expectation carries on that our young couples to hold resilient honorable expectations and use for transparency, the unwavering had said in an announcement.Visa's putting their signature to while having FIFA in front of the 2008 whole world glass costs football's ruling your body $90 million held in a injuries agreement hire ousted mastercard,
#61
Saturday, September 20, 2014 5:53 AM
it's new recording think on the market 374,000 replications in just one week promptly after there June copy.
#32
Monday, September 15, 2014 1:10 PM
they certainly excite us a lot now as well. But it takes more than four wheels and a body to makes us feel excited now. In fact we need something new in a car to excite us all the while.Now though we like our car to look exciting we have other priorities as well and we cannot go on working towards enhancing our car. And that in any case will get very expensive in terms of the time
コンバース バッシュ http://www.merryknollcampground.com/images/converse-shoes-15.html
#30
Sunday, September 14, 2014 11:53 AM
certainly i might want to see Derrick went up by better for off the floor in her 1 Bulls jacket, as opposed to the recreational compliments we are currently encountering your guy be clothed in all the while ski the actual market bench, i do think ensures that it is in the welfare to still stay fixed.
#2
Sergey says:
Friday, August 1, 2014 2:24 AM
Монго умеет так много! А реальные аппы будут публиковаться? Особенно на nodejs?

Leave a Comment

Fields with * are required.

Please enter the letters as they are shown in the image above.
Letters are not case-sensitive.