Database
[{"collection":"users","count":100,"content":{"_id":{"type":"autoincrement","autoType":"int","startInt":0},"ranks":{"type":"array","minLength":0,"maxLength":8,"arrayContent":{"type":"int","minInt":0,"maxInt":19}}}},{"collection":"ranks","count":20,"content":{"_id":{"type":"autoincrement","autoType":"int","startInt":0},"name":{"type":"stringFromParts","parts":[{"type":"constant","constVal":"Rank "},{"type":"autoincrement","autoType":"int","startInt":0}]},"permissions":{"type":"int","minInt":0,"maxInt":255}}}]
Query
db.users.aggregate([{"$match":{"_id":42}},{"$lookup":{"from":"ranks","localField":"ranks","foreignField":"_id","pipeline":[{"$project":{"_id":0,"permissions":1}}],"as":"permissions"}},{"$set":{"permissions":{"$function":{"body":"function(perms) {return perms.reduce((prevV, currV) => prevV | currV, 0)}","args":["$permissions.permissions"],"lang":"js"}}}}])