Skip to content

Instantly share code, notes, and snippets.

@jeffyuhaoliu
jeffyuhaoliu / final4-js
Created October 17, 2013 23:51
MongoDB & Node.js Final 4 - Enhancing the Blog to support viewers liking certain comments In this problem, you will be enhancing the blog project to support users liking certain comments and the like counts showing up the in the permalink page. Start by downloading the code in Final4.zip and loading up the blog dataset posts.json. The user inter…
/* The PostsDAO must be constructed with a connected database object */
function PostsDAO(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 PostsDAO)) {
console.log('Warning: PostsDAO constructor called without "new" operator');
return new PostsDAO(db);
}
@jeffyuhaoliu
jeffyuhaoliu / final3.js
Created October 17, 2013 23:48
MongoDB & Node.js Final 3 - In this problem you will update a document in the Enron dataset to illustrate your mastery of updating documents from the shell. Please add the email address "mrpotatohead@mongodb.com" to the list of addresses in the "headers.To" array for the document with "headers.Message-ID" of "<8147308.1075851042335.JavaMail.evan…
use enron
db.messages.update(
{'headers.Message-ID': '<8147308.1075851042335.JavaMail.evans@thyme>'},
{$addToSet: {
"headers.To": "mrpotatohead@mongodb.com"
}},
{multi: true}
)
@jeffyuhaoliu
jeffyuhaoliu / final2.js
Created October 17, 2013 23:47
MongoDB & Node.js Final 2 - Please use the Enron dataset you imported for the previous problem. For this question you will use the aggregation framework to figure out pairs of people that tend to communicate a lot. To do this, you will need to unwind the To list for each message. This problem is a little tricky because a recipient may appear mor…
use enron
db.messages.aggregate([
{
$unwind: "$headers.To"
},
{
$project: {
"_id": 1,
"headers.From": 1,
"headers.To": 1
@jeffyuhaoliu
jeffyuhaoliu / final1.js
Created October 17, 2013 23:43
MongoDB & Node.js Finals 1 - Construct a query to calculate the number of messages sent by Andrew Fastow, CFO, to Jeff Skilling, the president. Andrew Fastow's email addess was andrew.fastow@enron.com. Jeff Skilling's email was jeff.skilling@enron.com.
use enron
db.messages.find({$and: [{"headers.From": "andrew.fastow@enron.com"}, {"headers.To": "jeff.skilling@enron.com"}]}).count()
@jeffyuhaoliu
jeffyuhaoliu / final7-1.js
Last active December 25, 2015 20:19
MongoDB & Node.js Class Finals 7 - Your task is to write a program to remove every image from the images collection that appears in no album. Or put another way, if an image does not appear in at least one album, it's an orphan and should be removed from the images collection.
use photosharing
db.images.aggregate([
{
$unwind: "$tags"
},
{
$match: { tags: 'kittens'}
},
{
$group: {
@jeffyuhaoliu
jeffyuhaoliu / SampleCreateReplicaSet
Last active December 23, 2015 05:59
Week 6 - Sample code to create MongoDB Replica Set
sudo bash < create_replica_set.sh
mongo --port 27018 < init_replica.js
@jeffyuhaoliu
jeffyuhaoliu / 5.3
Created September 14, 2013 17:33
Node.js MongoDB HW#5.3 - Use aggregation framework to calculate the class with the best average student performance. This involves calculating an average for each student in each class of all non-quiz assessments and then averaging those numbers to get a class average. To be clear, each student's average includes only exams and homework grades. …
use hw5
db.grades.aggregate([
{
$unwind: "$scores"
}
,{
$match: {
"scores.type": { $ne: "quiz" }
}
}
@jeffyuhaoliu
jeffyuhaoliu / 5.2
Created September 14, 2013 07:29
Node.js MongoDB HW#5.2 - Use aggregation framework calculate the average population of cities in California (abbreviation CA) and New York (NY) (taken together) with populations over 25,000. Note: To run this in Terminal, type in... mongo < [file name of this .js file]. This should run the command of the .js file you created with the mongo shell…
use hw5
db.zips.aggregate([
/* First group via sum up of the values of the cities with the same name
this is because a city will have multiple zips therefore,
multiple entries of cities with the same name */
{
$group: {
_id: { state: "$state", city: "$city"},
pop: { $sum: "$pop" }
}
@jeffyuhaoliu
jeffyuhaoliu / 5.1
Created September 13, 2013 07:27
Node.js MongoDB HW#5.1 - Use aggregation framework to calculate the author with the greatest number of comments. Note: To run this in Terminal, type in... mongo < [file name of this .js file]. This should run the command of the .js file you created with the mongo shell and return back results in Terminal.
use blog
db.posts.aggregate([{
$unwind: "$comments"
}, {
$group: {
_id: "$comments.author",
postcount: {
$sum: 1
}
}
@jeffyuhaoliu
jeffyuhaoliu / 4.3
Created September 8, 2013 17:49
Node.js MongoDB HW#4.3 - This array shows the indexes that were created to answer HW#4.3. Note: To create an index, run the following command... db.posts.ensureIndex({date: -1}) db.posts.ensureIndex({tags: 1, date: -1}) db.posts.ensureIndex({permalink: 1})
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"ns" : "blog.posts",
"name" : "_id_"
},
{