Database
db={"user":[{_id:ObjectId("5f3a90110132e115db700201"),username:"A",name:"A",avatar:"",last_seen:new Date(),status:true}],"hsVideo":[{_id:1,name:"Hs Video",url:"",uploadedBy:ObjectId("5f3a90110132e115db700201"),status:true}],"fsVideo":[{_id:1,name:"Fs Video",url:"",uploadedBy:ObjectId("5f3a90110132e115db700201"),status:true}],"histories":[{user_id:ObjectId("5f3a90110132e115db700201"),hs_videoId:null,fs_videoId:1,action:"viewed"},{user_id:ObjectId("5f3a90110132e115db700201"),hs_videoId:1,fs_videoId:null,action:"viewed"},{user_id:ObjectId("5f3a90110132e115db700201"),hs_videoId:null,fs_videoId:1,action:"downloaded"},{user_id:ObjectId("5f3a90110132e115db700201"),hs_videoId:1,fs_videoId:null,action:"downloaded"}]}
Query
db.user.aggregate([{$match:{_id:ObjectId("5f3a90110132e115db700201")}},{$lookup:{from:"histories",let:{user_id:"$_id"},as:"HsVideos",pipeline:[{$match:{$expr:{$eq:["$user_id","$$user_id"]}}},{$lookup:{from:"hsVideo",let:{hs_videoId:"$hs_videoId"},as:"hs_videoId",pipeline:[{$match:{$expr:{$eq:["$$hs_videoId","$_id"]}}},{$lookup:{from:"user",localField:"uploadedBy",foreignField:"_id",as:"uploadedBy"}},{$unwind:"$uploadedBy"}]}},{$unwind:"$hs_videoId"},{$group:{_id:"$action",v:{$push:{_id:"$_id",url:"$hs_videoId.url",name:"$hs_videoId.name",uploadedBy:"$hs_videoId.uploadedBy"}}}},{$project:{_id:0,k:"$_id",v:1}}]}},{$lookup:{from:"histories",let:{user_id:"$_id"},as:"FsVideos",pipeline:[{$match:{$expr:{$eq:["$user_id","$$user_id"]}}},{$lookup:{from:"fsVideo",let:{fs_videoId:"$fs_videoId"},as:"fs_videoId",pipeline:[{$match:{$expr:{$eq:["$$fs_videoId","$_id"]}}},{$lookup:{from:"user",localField:"uploadedBy",foreignField:"_id",as:"uploadedBy"}},{$unwind:"$uploadedBy"}]}},{$unwind:"$fs_videoId"},{$group:{_id:"$action",v:{$push:{_id:"$_id",url:"$fs_videoId.url",name:"$fs_videoId.name",uploadedBy:"$fs_videoId.uploadedBy"}}}},{$project:{_id:0,k:"$_id",v:1}}]}},{$addFields:{FsVideos:{$arrayToObject:"$FsVideos"},HsVideos:{$arrayToObject:"$HsVideos"}}}])