MongoDB Aggregations — Part 1

Nate Hall
Nate Hall
Jun 25 · 5 min read

Why put data in MongoDB?

How to aggregate data in MongoDB

db[‘Collection_to_query’].find({},{“metric”:1,”_id”:0}).limit(10000)
{ $group: { _id: <expression>, <field1>: { <accumulator1> : <expression1> }, … } }
{ $filter: { input: <array>, as: <string>, cond: <expression> } }
{ $match: { $expr: { <aggregation expression> } } }
{ $limit: <positive integer> }
{ $project: { <specification(s)> } }

Learning & Practicing MongoDB aggregations using Knowi

db['sendingActivity'].aggregate([ {
"$group" : {
"customer" : {
"$first" : "$customer"
},
"Sum of sent" : {
"$sum" : "$sent"
},
"_id" : null
}
}, {
"$project" : {
"customer" : 1,
"Sum of sent" : 1,
"_id" : 0
}
}, {
"$limit" : 10000

} ]
db['sendingActivity'].aggregate([ {
"$group" : {
"customer" : {
"$first" : "$customer"
},
"Sum of sent" : {
"$sum" : "$sent"
},
"_id" : {
"date" : "$date"
}
}
}, {
"$project" : {
"customer" : 1,
"date" : "$_id.date",
"Sum of sent" : 1,
"_id" : 0
}
}, {
"$limit" : 10000
} ])

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade