I inherited a mongo query that I've been tasked to put into a GCP cloud function. The cloud function is getting a mongo timeout when it hits this query. When running locally, it takes quite a while as well ~ 40 seconds - but it does finish. I could use help in 2 ways here;
- Does my prognosis about why the cloud function is timing out on mongo sound right? Or are there cloud function settings to adjust?
- Any suggestions for optimizing it are welcome. ChatGPT suggests combining $project stages, removing $group stages, and limiting return fields to only those necessary.
cash_balances_pipeline = [
{
'$match': {
'audit.createdDate': {
'$gte': datetime(2022, 1, 1, 10, 0, 0, tzinfo=timezone.utc),
'$lt': end_date
},
'status': 'completed'
}
},
{
'$unwind': {
'path': '$valueMap.bonusFunds',
'preserveNullAndEmptyArrays': True
}
},
{
'$project': {
'type': 1,
'cents': {
'$ifNull': ['$valueMap.cents', 0]
},
'bonusFundsAwarded': {
'$cond': [
{ '$in': ['$valueMap.bonusFunds.promotionType', ['riskFreeEntry']] },
{ '$ifNull': ['$valueMap.bonusFunds.value', 0] },
0
]
},
'bonusFundsUsed': {
'$cond': [
{ '$in': ['$valueMap.bonusFunds.promotionType', ['riskFreeEntry']] },
{ '$ifNull': ['$valueMap.bonusFunds.amountUsed', 0] },
0
]
}
}
},
{
'$group': {
'_id': '$type',
'cents': { '$sum': '$cents' },
'bonusFundsAwarded': { '$sum': '$bonusFundsAwarded' },
'bonusFundsUsed': { '$sum': '$bonusFundsUsed' },
'totalCents': {
'$sum': {
'$add': ['$cents', '$bonusFundsAwarded', { '$multiply': ['$bonusFundsUsed', -1] }]
}
}
}
},
{
'$project': {
'totalCents': { '$multiply': ['$totalCents', 0.01] },
'bonusFundsAwarded': 1,
'bonusFundsUsed': 1
}
},
{
'$sort': { '_id': 1 }
}
]
Although this mongo query could benefit from optimizing, the issue was in Network Settings on the Google Cloud function (see photo). Thank you @rickhg12hs for ideas about improving the query; learning more about mongo is always valuable.