Getting the following error:
raise OperationFailure(errmsg, code, response, max_wire_version) pymongo.errors.OperationFailure: Out of memory, full error: {'operationTime': Timestamp(1650998892, 1), 'ok': 0.0, 'errmsg': 'Out of memory', 'code': 139, 'codeName': 'JSInterpreterFailure', '$clusterTime': {'clusterTime': Timestamp(1650998892, 1), 'signature': {'hash': b'Z\x93L\x033\xe6 \x82|\x82wA\xf3Bh\xe7\xae\xce\x0c\x17', 'keyId': 7031229705458024454}}}
The following query does to work in PyMongo.
aggregate_pipeline = [
{
'$project': {
'data': "$$ROOT"
}
},
{
"$project": {
"flattenKeys": {
"$function": {
"body": "function flatten(dataList, keysToHandle){while(keysToHandle.length){origKeyData = keysToHandle.shift();for (const newKey of Object.keys(origKeyData.objData)){keysToHandle.push({objData:origKeyData.objData[newKey],keyData:`${origKeyData.keyData}.${newKey}`});dataList.push(`${origKeyData.keyData}.${newKey}`);}}return dataList}",
"args": [
[],
[
{
'objData': "$data",
'keyData': "data"
}
]
],
"lang": "js"
}
}
}
},
{
'$project': {
'matchingKeys': {
'$filter': {
'input': "$flattenKeys",
'as': "item",
'cond': {
'$regexMatch': {
'input': "$$item",
'regex': fields
}
}
}
}
}
}]
result = collection.aggregate(aggregate_pipeline, allowDiskUse=True)
print(list(result))