db.getCollection('movements').aggregate( [ { "$match": { "idapp": "13", "$or": [ { "accountFromId": 1 }, { "accountToId": 1 } ] } }, { "$lookup": { "from": "accounts", "localField": "accountFromId", "foreignField": "_id", "as": "accfrom" } }, { "$lookup": { "from": "accounts", "localField": "accountToId", "foreignField": "_id", "as": "accto" } }, { "$unwind": "$accfrom" }, { "$unwind": "$accto" }, { "$lookup": { "from": "users", "let": { "username": "$accfrom.username", "idapp": "$accfrom.idapp" }, "pipeline": [ { "$match": { "$expr": { "$and": [ { "$eq": [ "$$username", "$username" ] }, { "$eq": [ "$$idapp", "$idapp" ] } ] } } } ], "as": "userfrom" } }, { "$unwind": "$userfrom" }, { "$lookup": { "from": "users", "let": { "username": "$accto.username", "idapp": "$accto.idapp" }, "pipeline": [ { "$match": { "$expr": { "$and": [ { "$eq": [ "$$username", "$username" ] }, { "$eq": [ "$$idapp", "$idapp" ] } ] } } } ], "as": "userto" } }, { "$unwind": "$userto" }, { "$project": { "transactionDate": 1, "amount": 1, "causal": 1, "userfrom.username": 1, "userfrom.profile.img": 1 "userto.username": 1, "userto.profile.img": 1 } } ])