learnyoumongo
Advanced tools
Comparing version 1.1.9 to 2.0.0
@@ -21,3 +21,3 @@ Next up is aggregation. Aggregation allows one to do things like | ||
In this exercise, we need to calculate the average price for all tshirts | ||
In this exercise, we need to calculate the average price for all documents in `prices` | ||
that have the size that will be passed as the first argument to your script. | ||
@@ -24,0 +24,0 @@ |
@@ -10,41 +10,61 @@ var mongo = require('mongodb').MongoClient | ||
var db, url = 'mongodb://localhost:27017/learnyoumongo' | ||
var base = 'mongodb://localhost:27017/' | ||
var exUrl = base + 'learnyoumongo' | ||
var solUrl = base + 'learnyoumongo2' | ||
var exdb, soldb | ||
exercise.addSetup(function(mode, cb) { | ||
var self = this | ||
this.submissionArgs = ['keys', '554a655c0639034860349353'] | ||
this.solutionArgs = ['keys', '554a655c0639034860349353'] | ||
mongo.connect(url, function(err, _db) { | ||
if (err) return cb(err) | ||
db = _db | ||
col = db.collection('keys') | ||
col.remove({}, function(err) { | ||
if (err) return cb(err) | ||
col.insert({ | ||
name: 'blah' | ||
, _id: '554a655c0639034860349353' | ||
}, cb) | ||
}) | ||
this.submissionArgs = ['learnyoumongo', 'keys', '554a655c0639034860349353'] | ||
this.solutionArgs = ['learnyoumongo2', 'keys', '554a655c0639034860349353'] | ||
mongo.connect(exUrl, function(err, _db) { | ||
if (err) return done(cb) | ||
exdb = _db | ||
insert(exdb, self.submissionArgs, cb) | ||
}) | ||
}) | ||
function insert(db, args, cb) { | ||
col = db.collection('keys') | ||
col.remove({}, { multi: true }, function(err) { | ||
if (err) { | ||
return cb(err) | ||
} | ||
col.insert({ | ||
name: 'blah' | ||
, _id: args[2] | ||
}, function(err) { | ||
if (err) { | ||
console.error('Error setting up exercise') | ||
console.error('This could be a bug in learnyoumongo') | ||
console.error('Please open an issue at:') | ||
console.error('https://github.com/evanlucas/learnyoumongo') | ||
return cb(err) | ||
} | ||
cb() | ||
}) | ||
}) | ||
} | ||
exercise.addProcessor(function(mode, cb) { | ||
this.submissionStdout.pipe(process.stdout) | ||
var args = mode === 'run' | ||
? this.submissionArgs | ||
: this.solutionArgs | ||
var args = this.solutionArgs | ||
return this.on('executeEnd', function() { | ||
verify(args, cb) | ||
verify(args, exdb, cb) | ||
}) | ||
}) | ||
exercise.addCleanup(function(mode, result, cb) { | ||
exdb.collection('keys').remove({}, cb) | ||
}) | ||
function verify(args, cb) { | ||
var col = db.collection(args[0]) | ||
col.find({ | ||
_id: args[1] | ||
}).toArray(function(err, docs) { | ||
function verify(args, db, cb) { | ||
var col = db.collection(args[1]) | ||
col.find({}).toArray(function(err, docs) { | ||
if (err) return cb(err) | ||
if (docs.length) return cb(null, false) | ||
if (docs.length) { | ||
console.error('Expected document to be removed', docs) | ||
return cb(null, false) | ||
} | ||
cb(null, true) | ||
@@ -51,0 +71,0 @@ }) |
This lesson involves removing a document with the given `_id`. | ||
The collection name will be passed as the first argument to your script. | ||
The database name will be accessible via `process.argv[2]`. | ||
The `_id` will be passed as the second argument to your script. | ||
The collection name will be passed as the second argument to your script. | ||
The `_id` will be passed as the third argument to your script. | ||
----------------------------------------------------------- | ||
@@ -8,0 +10,0 @@ ## HINTS |
var mongo = require('mongodb').MongoClient | ||
var url = 'mongodb://localhost:27017/learnyoumongo' | ||
var url = 'mongodb://localhost:27017/' + process.argv[2] | ||
mongo.connect(url, function(err, db) { | ||
if (err) throw err | ||
var collection = db.collection(process.argv[2]) | ||
var collection = db.collection(process.argv[3]) | ||
collection.remove({ | ||
_id: process.argv[3] | ||
_id: process.argv[4] | ||
}, function(err) { | ||
@@ -10,0 +11,0 @@ if (err) throw err |
@@ -10,13 +10,31 @@ var mongo = require('mongodb').MongoClient | ||
exercise = execute(exercise) | ||
var base = 'mongodb://localhost:27017/' | ||
var exUrl = base + 'learnyoumongo' | ||
var solUrl = base + 'learnyoumongo2' | ||
var exdb, soldb | ||
var db, url = 'mongodb://localhost:27017/learnyoumongo' | ||
exercise.addSetup(function(mode, cb) { | ||
this.submissionArgs = ['learnyoumongo'] | ||
this.solutionArgs = ['learnyoumongo2'] | ||
var count = 0, error | ||
function done(err) { | ||
count++ | ||
if (err) { | ||
error = err | ||
} | ||
exercise.addSetup(function(mode, cb) { | ||
if (mode === 'verify') { | ||
return mongo.connect(url, function(err, _db) { | ||
if (err) return cb(err) | ||
db = _db | ||
resetUsers(cb) | ||
}) | ||
if (count === 2) cb(error) | ||
} | ||
mongo.connect(exUrl, function(err, _db) { | ||
if (err) return done(err) | ||
exdb = _db | ||
resetUsers(exdb, done) | ||
}) | ||
mongo.connect(solUrl, function(err, _db) { | ||
if (err) return done(err) | ||
soldb = _db | ||
resetUsers(soldb, done) | ||
}) | ||
}) | ||
@@ -27,4 +45,4 @@ | ||
return this.on('executeEnd', function() { | ||
verifyUser(function(err) { | ||
cb(null, true) | ||
verifyUser(exdb, function(err, passed) { | ||
cb(null, passed) | ||
}) | ||
@@ -34,3 +52,3 @@ }) | ||
function resetUsers(cb) { | ||
function resetUsers(db, cb) { | ||
var users = db.collection('users') | ||
@@ -47,3 +65,3 @@ users.remove({}, function(err) { | ||
function verifyUser(cb) { | ||
function verifyUser(db, cb) { | ||
var users = db.collection('users') | ||
@@ -66,3 +84,3 @@ users.find({ | ||
exercise.emit('fail', 'Document has incorrect age property.' + | ||
'Expected: %s Actual: %s', chalk.green(40), chalk.red(doc.age)) | ||
'Expected: ' + chalk.green(40) + ' Actual: ' + chalk.red(doc.age)) | ||
cb(null, false) | ||
@@ -69,0 +87,0 @@ }) |
Here we are going to update a document in the `users` collection. | ||
The database name will be accessible via `process.argv[2]`. | ||
Say we have a user defined like: | ||
@@ -4,0 +6,0 @@ |
var mongo = require('mongodb').MongoClient | ||
var url = 'mongodb://localhost:27017/learnyoumongo' | ||
var url = 'mongodb://localhost:27017/' + process.argv[2] | ||
mongo.connect(url, function(err, db) { | ||
@@ -5,0 +5,0 @@ if (err) throw err |
{ | ||
"name": "learnyoumongo", | ||
"version": "1.1.9", | ||
"version": "2.0.0", | ||
"description": "Nodeschool workshop for MongoDB", | ||
@@ -5,0 +5,0 @@ "bin": { |
71126
665