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();
}
db.collection('grades').findOne(query, callback);
});
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.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 = { '$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);
cursor.sort([['State',1], ['Temperature',-1]]);
var state = '';
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) {
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');
function UsersDAO(db) {
"use strict";
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";
var salt = bcrypt.genSaltSync();
var password_hash = bcrypt.hashSync(password, salt);
var user = {'_id': username, 'password': password_hash};
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";
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");
invalid_password_error.invalid_password = true;
callback(invalid_password_error, null);
}
}
else {
var no_such_user_error = new Error("User: " + user + " does not exist");
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;