there are currently three collections user consume recharge
just want to completely modify the User collection instead of creating a temporary collection like lookup, is there any efficient way, after all, there is a lot of data.
currently used $replaceRoot
$mergeObjects
method can be implemented, but the speed is too slow and the CPU occupancy rate is extremely high
user = db.get_db_user()
recharge = db.get_db_recharge()
consume = db.get_db_consume()
lookup1 = {"$lookup": {
"from": "recharge",
"localField": "_id",
"foreignField": "uid",
"as": "recharge"
}}
lookup2 = {"$lookup": {
"from": "consume",
"localField": "_id",
"foreignField": "uid",
"as": "consume"
}}
replaceRoot1 = {"$replaceRoot": {"newRoot": {"$mergeObjects": [{"$arrayElemAt": ["$recharge", 0]}, "$$ROOT"]}}}
replaceRoot2 = {"$replaceRoot": {"newRoot": {"$mergeObjects": [{"$arrayElemAt": ["$consume", 0]}, "$$ROOT"]}}}
pipeline = [lookup1, lookup2, replaceRoot1, replaceRoot2]
result = user.aggregate(pipeline)
num = 0
for i in result:
num = num + 1
pprint.pprint(i)
print(num)