Database

db={userMaster:[{userName:"xxxx",roleId:1,fullName:"xxxxxxx"}],customerMaster:[{userName:"xxxx",address:"YYYY",firstName:"zzzzz",lastName:"ppppp"}]}

Query

db.userMaster.aggregate([{"$match":{"userName":{"$in":["xxxx","xx"]}}},{"$unionWith":{"coll":"customerMaster","pipeline":[{"$match":{"userName":{"$in":["xxxx","xx"]}}}]}},{$sort:{roleId:-1}},{$group:{_id:"$userName",doc:{$first:"$$ROOT"}}}])

Result