I need to group the objects inside of votes by mallId inside of a mongodb aggreate pipeline. Been searching for an answer for ages, but I am not too familiar with mongodb so I am kind of lost. Can anyone help me out?
[
{
"_id": "Specialty Retailer",
"votes": [
{
"mallId": "59ddea3718a03a4e81f289f5",
"count": 13
},
{
"mallId": "59ddea3718a03a4e81f289f5",
"count": 270
},
{
"mallId": "59ddea3718a03a4e81f289f5",
"count": 13
},
{
"mallId": "59ddea3718a03a4e81f289f5",
"count": 2
},
{
"mallId": "59ddea3718a03a4e81f289f5",
"count": 15
},
{
"mallId": "59ddea3718a03a4e81f289f5",
"count": 2
}
]
}
]
Accumulators are operators that maintain their state (e.g. totals, maximums, minimums, and related data) as documents progress through the pipeline. Use the $accumulator operator to execute your own JavaScript functions to implement behavior not supported by the MongoDB Query Language.
The $$ROOT variable contains the source documents for the group. If you'd like to just pass them through unmodified, you can do this by $pushing $$ROOT into the output from the group.
This means $first returns the first order type for the documents between the beginning of the partition and the current document.
One of the benefits of MongoDB's rich schema model is the ability to store arrays as document field values. Storing arrays as field values allows you to model one-to-many or many-to-many relationships in a single document, instead of across separate collections as you might in a relational database.
Given the following document structure
{
"_id" : ObjectId("5a020ea06216d24dd6050a07"),
"arr" : [
{
"_id" : "Specialty Retailer",
"votes" : [
{
"mallId" : "59ddea3718a03a4e81f289f5",
"count" : 13
},
{
"mallId" : "59ddea3718a03a4e81f289f5",
"count" : 270
}
]
}
]
}
this query should get you going:
db.collection.aggregate({
$unwind: "$arr"
}, {
$unwind: "$arr.votes"
}, {
$group: {
"_id": {
"_id": "$_id",
"arrId": "$arr._id",
"mallId": "$arr.votes.mallId"
},
"totalCount": {
$sum: "$arr.votes.count"
}
}
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With