-
Notifications
You must be signed in to change notification settings - Fork 340
MongoDB Aggregate
Created by Rafase282
Github | FreeCodeCamp | CodePen | LinkedIn | Medium Website | E-Mail
Next up is aggregation. Aggregation allows one to do things like calculate the sum of a field of multiple documents or the average of a field of documents meeting particular criteria.
Say you have a collection named prices. Each price document is modeled like so:
{
"name": "Tshirt",
"size": "S",
"price": 10,
"quantity": 12
"meta": {
"vendor": "hanes",
"location": "US"
}
}
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.
Use console.log()
to print the average price rounded to 2 decimal places to stdout after you have found it.
To use the aggregate()
function, one first needs the collection. The aggregate()
function takes an array of objects as the first argument.
This array will contain the different pipelines for the aggregation. To read more about pipelines, please visit Aggregation.
The two main pipeline stages we will use will be $match and $group.
$match is used similar to the way a query is done. It allows us to select the documents that meet certain criteria.
Ex.
var match = { $match: { status: 'A' } }
The above example will match all of the documents that have a status property equal to A.
$group is what allows us to run operations on certain properties.
So, say we wanted to get the sum of the values of the property value where status is equal to A and have it placed in the total property.
Ex.
// [
// { status: 'A', value: 1 },
// { status: 'B', value: 2 },
// { status: 'A', value: 10 }
// ]
collection.aggregate([
{ $match: { status: 'A' }}
, { $group: {
_id: 'total' // This can be an arbitrary string in this case
, total: {
// $sum is the operator used here
$sum: '$value'
}
}}
]).toArray(function(err, results) {
// handle error
console.log(results)
// => [
// => { _id: 'total', total: 11 }
// => ]
})
Other operators used in the $group stage include:
$avg
$first
$last
$max
$min
$push
$addToSet
Thanks for visiting, if you like this please feel free to star my repo, follow me or even contact me about contributing as it will be a lot of work and having help would be cool.
- HTML5 and CSS
- Responsive Design with Bootstrap
- Gear up for Success
- jQuery
- Basic JavaScript
- Object Oriented and Functional Programming
- Basic Algorithm Scripting
- Basic Front End Development Projects
- Intermediate Algorithm Scripting
- JSON APIs and Ajax
- Intermediate Front End Development Projects
- Claim Your Front End Development Certificate
- Upper Intermediate Algorithm Scripting
- Automated Testing and Debugging
- Advanced Algorithm Scripting
- AngularJS (Legacy Material)
- Git
- Node.js and Express.js
- MongoDB
- API Projects
- Dynamic Web Applications
- Claim Your Back End Development Certificate
- Greefield Nonprofit Project 1
- Greefield Nonprofit Project 2
- Legacy Nonprofit Project 1
- Legacy Nonprofit Project 2
- Claim your Full Stack Development Certification
- Whiteboard Coding Interview Training
- Critical Thinking Interview Training
- Mock Interview 1
- Mock Interview 2
- Mock Interview 3