I have a collection of documents as follows, on which I would like to perform a lookup over another one and an aggregation:
{
transactions: {
Codice articolo:"039180094"
Tipo di acquisto:"D"
},
transaction_value:9.912
}
Current aggregation is:
[{
'$lookup': {
'from': 'product_to_categories',
'localField': 'transactions.Codice articolo',
'foreignField': 'MIN_SAN',
'as': 'product_info'
}
}, {
'$unwind': {
'path': '$product_info',
'preserveNullAndEmptyArrays': False
}
}, {
'$addFields': {
'prod_category': {
'$toString': '$product_info.DESC_GRU1'
}
}
}, {
'$project': {
'_id': 0,
'transactions.Tipo di acquisto': 1,
'prod_category': 1,
'transaction_value': 1
}
}, {
'$group': {
'_id': [
'$transactions.Tipo di acquisto', '$prod_category'
],
'financial_impact': {
'$sum': '$transaction_value'
}
}
}, {
'$sort': {
'financial_impact': -1
}
}]
The aggregation in Compass correctly runs until the group step, which causes a timeout error or runs indefinitely even using Compass' sample mode.
I've tried to exclude the lookup operation from the aggregation and it correctly performs the group step (to illustrate, grouping only by "$transactions.Tipo di acquisto"). Likewise, raising Compass' "Max time" value to 90000000 but the result doesn't change.
As a final note, the variable "Tipo di acquisto" can either be equal to "D" or "Nan" (Double type).
How could I fix that?
_id
as an object with keys, not as array – nimrod serok