Skip to content

Instantly share code, notes, and snippets.

@terakilobyte
Created November 16, 2017 13:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save terakilobyte/30402cef2aeed9e07b35ee0d05d0b94b to your computer and use it in GitHub Desktop.
Save terakilobyte/30402cef2aeed9e07b35ee0d05d0b94b to your computer and use it in GitHub Desktop.
validatePipeline1.js
function validateLab1 (pipeline) {
var aggregations = db.getSiblingDB("aggregations")
if (!pipeline) {
print("var pipeline isn't properly set up!")
} else {
var studentSubmission = aggregations.movies.aggregate(pipeline)
try {
var keys = Object.keys(Object.getPrototypeOf(studentSubmission)).length
var batchSize = studentSubmission._batch.length
var totalReturned = studentSubmission.itcount()
var data = studentSubmission
.toArray()
.reduce(
(acc, curr) => acc + (curr.year * batchSize - curr.viewerVotes),
totalReturned
)
if (totalReturned === 23) {
print("Answer is", data % keys)
} else {
print("You aren't returning the correct number of documents")
}
} catch (e) {
if (e instanceof TypeError && e.message.includes("batch is undefined")) {
print("It doesn't appear you're using the aggregation framework")
} else {
print(e.message)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment