Reputation: 314
Imagine a data set like this:
db.test.insertMany([
{ '_id':1, 'name':'aa1', 'price':10, 'quantity': 2, 'category': ['coffe'] },
{ '_id':2, 'name':'aa2', 'price':20, 'quantity': 1, 'category': ['coffe', 'snack'] },
{ '_id':3, 'name':'aa3', 'price':5, 'quantity':10, 'category': ['snack', 'coffe'] },
{ '_id':4, 'name':'aa4', 'price':5, 'quantity':20, 'category': ['coffe', 'cake'] },
{ '_id':5, 'name':'aa5', 'price':10, 'quantity':10, 'category': ['animal', 'dog'] },
{ '_id':6, 'name':'aa6', 'price':5, 'quantity': 5, 'category': ['dog', 'animal'] },
{ '_id':7, 'name':'aa7', 'price':5, 'quantity':10, 'category': ['animal', 'cat'] },
{ '_id':8, 'name':'aa8', 'price':10, 'quantity': 5, 'category': ['cat', 'animal'] },
]);
I'm trying to make a query with this result (or something like it):
[
{ ['animal', 'dog'], 125 },
{ ['animal', 'cat'], 100 },
{ ['coffe', 'cake'], 100 },
{ ['coffe', 'snack'], 70 },
{ ['coffe'], 20 }
]
Meaning that it is:
I've tried everything I know (which is very limited) and googled for days without getting anywhere. Is this even possible in an aggregate query or do I have find a different way?
Upvotes: 0
Views: 64
Reputation: 10737
I suppose you need something like this:
db.collection.aggregate([
{
$unwind: "$category"
},
{
$sort: {
_id:-1,
category: -1
}
},
{
$group: {
_id: "$_id",
category: {
$push: "$category"
},
price: {
$first: "$price"
},
quantity: {
$first: "$quantity"
}
}
},
{
$group: {
_id: "$category",
sum: {
$sum: {
$multiply: [
"$price",
"$quantity"
]
}
}
}
},
{
$project: {
mySet: "$_id",
total: "$sum"
}
},
{
$sort: {
total: -1
}
}
])
Explained:
Please, note output has name for the set and total for the sum to be valid JSON since it is not possible to have the output as {[X,Y],Z} and need to be {m:[X,Y],z:Z}
Upvotes: 2
Reputation: 6629
db.collection.aggregate([
{
"$match": {}
},
{
"$group": {
"_id": {
$function: {
body: "function(arr) { return arr.sort((a,b) => a.localeCompare(b))}",
args: [ "$category" ],
lang: "js"
}
},
"sum": {
"$sum": { "$multiply": [ "$price", "$quantity" ] }
}
}
},
{
"$sort": { sum: -1 }
}
])
In mongodb 5.2 version you can use $sortArray instead of function sort that I used.
Upvotes: 2