Skip to content

Instantly share code, notes, and snippets.

@jeffyuhaoliu
jeffyuhaoliu / gist:6004713
Last active December 19, 2015 19:18 — forked from janbaer/gist:5045798
- Fixed a bug where string value of double quote causes an error "The converted JSON string is in bad format."
param
(
[string] $inputFile,
[string] $outputFile
)
if (($inputFile -eq $null) -or ($outputFile -eq $null)) {
"usage: convert_csv_to_json.ps1 [inputFile] [outputFile]"
return;
}
@jeffyuhaoliu
jeffyuhaoliu / 3.1
Created August 29, 2013 06:14
Node.js MongoDB HW#3.1
var MongoClient = require('mongodb').MongoClient;
MongoClient.connect('mongodb://localhost:27017/school', function(err, db) {
if(err) throw err;
db.collection('students').aggregate(
{'$unwind':'$scores'},
{'$match':{'scores.type': 'homework'}},
{'$group':{'_id':'$_id',
// 'scores':{$addToSet:'$scores.score'},
@jeffyuhaoliu
jeffyuhaoliu / 3.3
Last active December 22, 2015 11:38
Node.js MongoDB HW#3.3
posts.update({"permalink": permalink}, {$push: { "comments": comment }}, function(err, doc) {
if (err) {
return callback(err, null)
}
else {
console.dir(doc + " : Comments added!")
return callback(null, doc);
}
});
@jeffyuhaoliu
jeffyuhaoliu / 3.2
Created September 6, 2013 17:02
Node.js MongoDB HW#3.2
posts.insert(post, function(err, doc){
if (!err) {
console.log("Inserted new post");
return callback(null, permalink);
}
return callback(err, null)
});
@jeffyuhaoliu
jeffyuhaoliu / 4.4
Created September 8, 2013 06:56
Node.js MongoDB HW#4.4 - This query, queries the profile data, looking for all queries to the students collection in the database school2, sorted in order of decreasing latency.
db.profile.find({ns:/school2.students/}, {millis: 1}).sort({millis: -1}).pretty()
@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_"
},
{
@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 / 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.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 / 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