[{$match: { _id: ObjectId('5efed80a0816af68744e16c4'), type:"program" }}, {$addFields: { 'seasons': { $size: '$assets' } }}, {$unwind: { path: '$assets', preserveNullAndEmptyArrays: true }}, {$addFields: { 'translations1': { $filter: { input: '$translations', as: 'translations', cond: { $and: [ { $eq: [ '$$translations.language', 'en' ] } ] } } } }}, {$addFields: { 'hlghdr': { $cond: { 'if': { $in: [ 'hlg hdr', '$hdr' ] }, then: true, 'else': false } }, '4k': { $cond: { 'if': { $in: [ '4k', '$resolution' ] }, then: true, 'else': false } }, 'dur': '$duration', 'title': { $arrayElemAt: [ '$translations1.details.title', 0 ] }, 'description': { $arrayElemAt: [ '$translations1.details.description', 0 ] }, }}, {$project: { items: 0, updated_on: 0, updated_by: 0 }}, {$addFields: { 'images': { $cond: [ { $ifNull: [ '$images', false ] }, '$images', [] ] },'assets.details': { $cond: [ { $ifNull: [ '$assets.details', false ] }, '$assets.details', [] ] } }}, {$addFields: { 'episodes': { $size: '$assets.details' } }}, {$unwind: { path: '$assets.details', preserveNullAndEmptyArrays: true }}, {$project: { 'category': 0, 'duration': 0, 'resolution': 0, 'translations': 0, 'translations1': 0, 'hdr': 0, 'items': 0, 'geo': 0, 'tags': 0, 'metadata': 0, 'schedule_on': 0, 'produced_on': 0, 'updated_on': 0, 'status': 0, 'friendlyname': 0 }}, {$addFields: { 'image': { $filter: { input: '$images', as: 'images', cond: { $and: [ { $eq: [ '$$images.aspect', '16:9' ] } ] } } } }}, {$addFields: { 'image': '$image.image', }}, {$project: { 'images': 0 }}, {$unwind: { path: '$image', preserveNullAndEmptyArrays: true }}, {$addFields: { 'image': { $cond: [ { $ifNull: [ '$image', false ] }, '$image', null ] } }}, {$addFields: { "assets.details.video": {$toObjectId:"$assets.details.video"} }}, {$lookup: { from: 'assets', localField:'assets.details.video', foreignField: '_id', as: 'data' }}, {$addFields: { 'watchlist':null, 'progress':null, 'data.watchlist': null, 'data.progress': null }}, {$unwind: { path: "$data", preserveNullAndEmptyArrays: true }}, {$addFields: {'data': { $cond: [ { $ifNull: [ '$data', false ] }, '$data', [] ] } }}, {$lookup: { from: 'users', localField: 'data._id', foreignField: 'watched.asset_id', as: 'users' }}, {$addFields: { "data.dur": "$data.duration", 'data.translations1': { $filter: { input: '$data.translations', as: 'translations', cond: { $and: [ { $eq: [ '$$translations.language', 'en' ] } ] } } } }}, {$addFields: { 'data.hlghdr': { $cond: { 'if': { $in: [ 'hlg hdr', '$data.hdr' ] }, then: true, 'else': false } }, 'data.4k': { $cond: { 'if': { $in: [ '4k', '$data.resolution' ] }, then: true, 'else': false } }, 'data.dur': '$data.duration', 'data.title': { $arrayElemAt: [ '$data.translations1.details.title', 0 ] }, 'data.description': { $arrayElemAt: [ '$data.translations1.details.description', 0 ] } }}, {$addFields: { 'data.images': { $cond: [ { $ifNull: [ '$data.images', false ] }, '$data.images', null ] } }}, {$addFields: { 'data.image': { $filter: { input: '$data.images', as: 'images', cond: { $and: [ { $eq: [ '$$images.aspect', '16:9' ] } ] } } } }}, {$addFields: { "data.image": { $cond: { if: { $eq: [ [[]],"$data.image" ] }, then: null, else: "$data.image" } }, }}, {$addFields: { 'data.image': '$data.image.image', 'data.episode':"$assets.details.episode", 'data.season':"$assets.season", "data.seasons":null, "data.episodes":null, episode:null }}, {$unwind: { path: "$users", preserveNullAndEmptyArrays: true }}, {$addFields: {progress: {$filter: { input: '$users.watched', as: 'watched', cond: {$eq: ['$$watched.asset_id', '$data._id']} }} }}, {$unwind: { path: '$data.image', preserveNullAndEmptyArrays: true }}, {$addFields: { 'data.image': { $cond: [ { $ifNull: [ '$data.image', false ] }, '$data.image', null ] } }}, {$addFields: { "data.image": { $cond: { if: { $eq: [ [[[]]],"$data.image" ] }, then: null, else: "$data.image" } },"data.dur": { $cond: { if: { $eq: [ [[]],"$data.dur" ] }, then: null, else: "$data.dur" } },"data.title": { $cond: { if: { $eq: [ [[]],"$data.title" ] }, then: null, else: "$data.title" } },"data.description": { $cond: { if: { $eq: [ [[]],"$data.description" ] }, then: null, else: "$data.description" } }, }}, {$unwind: { path: "$progress", preserveNullAndEmptyArrays: true }}, {$addFields: { 'data.progress':"$progress.progress" }}, {$addFields: { "data.progress": { "$cond": [ { "$ifNull": ["$progress", false] },"$progress.progress", 0 ] } } }, {$project: { "data.category":0, "data.duration":0, "data.resolution":0, "data.translations":0, "data.translations1":0, "data.hdr":0, "data.items":0, "data.geo":0, "data.tags":0, "data.metadata":0, "data.schedule_on":0, "data.produced_on":0, "data.updated_on":0, "data.status":0, "data.assets":0, "data.friendlyname":0, "data.images":0, "assets":0 }}, {$unwind: { path: "$data", preserveNullAndEmptyArrays: true }}, {$sort: { 'data.episode': 1 }}, {$group: { _id:"$data.episode", id:{ $first:"$_id" }, title: { $first: '$title' }, description: { $first: '$description' }, episode:{ $first:'$episode' }, seasons: { $first: '$seasons' }, episodes:{ $first:"$episodes" }, dur: { $first: '$dur' }, "4k": { $first: '$4k' }, hlghdr: { $first: '$hlghdr' }, progress:{$first:null}, image:{ $first:"$image" }, watchlist:{ $first:"$watchlist" }, type: { $first: '$type' }, data: { $addToSet: '$data' } }}, {$unwind: { path: '$data', includeArrayIndex: 'index', preserveNullAndEmptyArrays: true }}, {$sort: { "_id": 1 }}, {$group: { _id:"$id", title: { $first: '$title' }, description: { $first: '$description' }, episode:{ $first:'$episode' }, seasons: { $first: '$seasons' }, episodes:{ $first:"$episodes" }, dur: { $first: '$dur' }, "4k": { $first: '$4k' }, hlghdr: { $first: '$hlghdr' }, progress:{$first:null}, image:{ $first:"$image" }, watchlist:{ $first:"$watchlist" }, type: { $first: '$type' }, data: { $push: '$data' } }}]