Wednesday, 2 February 2022

rootReplace in mongoDB

//root replace in mongodb [ { '$match': { 'parent_id': 0 } }, { '$addFields': { 'parent_id': { '$toString': '$_id' } } }, { '$lookup': { 'from': 'PAYMENTMODES', 'localField': 'parent_id', 'foreignField': 'parent_id', 'as': 'data' } }, { '$addFields': { 'final_data': { '$map': { 'input': '$data', 'as': 'g', 'in': { 'k': '$$g.title', 'v': { 'is_recurring': '$$g.is_recurring', 'image': '$$g.image' } } } } } }, { '$replaceRoot': { 'newRoot': { '$arrayToObject': { '$concatArrays': [ [ { 'k': '$title', 'v': { '$arrayToObject': { '$concatArrays': [ [ { 'k': 'is_recurring', 'v': '$is_recurring' }, { 'k': '$title', 'v': { '$arrayToObject': { '$concatArrays': [ '$final_data' ] } } } ] ] } } } ] ] } } } } ] // replace root with key value https://stackoverflow.com/questions/66237048/how-to-replace-the-root-with-object-value-in-mongodb ====================================================================================================================================================================== ====================================================================================================================================================================== db.packages.update( {"catalogue.0":"58356a157f8b9a50613c5ec7",record_date:{"$gte": new Date('2019-02-14T00:00:00Z'),"$lte": new Date('2019-02-16T00:00:00Z')}}, { $set : { "streaming_mode" : "vodafone_android"}}, {multi: true}); db.SESSION.update({"catlogue.0":"58356a157f8b9a50613c5ec7"},{ $set : { "device_token" : ""}}, {multi: true}) db.SESSION.update({"catlogue.0":"58356a157f8b9a50613c5ec7"},{ $set : { "device_token" : ""}}, {multi: true}) db.SESSION.update({"catlogue.0":"58356a157f8b9a50613c5ec7"},{ $set : { "device_token" : ""}}, {multi: true}) db.SESSION.find({"catlogue.0":"58356a157f8b9a50613c5ec7" ,"device_token" : ""}) db.SESSION.find({"catlogue.0":"58356a157f8b9a50613c5ec7","device_token": {$exists: true, $not: {$size: 0}}}) db.SESSION.find({"catlogue.0":"58356a157f8b9a50613c5ec7","device_token": {$exists: true, $ne:''}}) {"streaming_mode" : "vodafone_android","asset_code" : "CH689","record_date" : ISODate("2019-06-15T18:30:00.000+0000")} db.packages.count( {"streaming_mode" : "vodafone_android","asset_code" : "CH689",record_date:{"$gte": new Date('2019-06-15T00:00:00Z'),"$lte": new Date('2019-06-15T23:59:00Z')}} ) db.packages.count( {"content_type":"LIVE","os_name" : "android","asset_code" : "CH689",record_date:{"$gte": new Date('2019-06-15T00:00:00Z'),"$lte": new Date('2019-06-15T23:59:00Z')}} ) db.packages.find( {"duration":{"$gte":30},"asset_code":{"$in":["CH370","CH369","CH368"]},"streaming_mode" : "vodafone_android","msisdn":/VFPlay/,record_date:{"$gte": new Date('2018-02-22T00:00:00Z'),"$lte": new Date('2019-02-23T00:00:00Z')}} ) db.packages.find( {"msisdn":/VFPlay/,record_date:{"$gte": new Date('2019-02-14T00:00:00Z'),"$lte": new Date('2018-07-16T00:00:00Z')}} ) db.packages.find( {"msisdn":/VFPlay/,"streaming_mode" => "vodafone_android",record_date:{"$gte": new Date('2019-02-23T00:00:00Z'),"$lte": new Date('2019-02-23T00:00:00Z')}} ) db.packages.find( {"msisdn":/VFPlay/,record_date:{"$gte": new Date('2019-02-14T00:00:00Z'),"$lte": new Date('2019-02-16T00:00:00Z')}} ) "msisdn":/VFPlay/,"streaming_mode" : "vodafone_android" {"msisdn":/VFPlay/,record_date:{"$gte": new Date('2019-02-14T00:00:00Z'),"$lte": new Date('2019-02-16T00:00:00Z')} db.packages.update( {"msisdn":/VFPlay/,record_date:{"$gte": new Date('2019-02-14T00:00:00Z'),"$lte": new Date('2019-02-16T00:00:00Z')}}, { $set : { "streaming_mode" : "vodafone_android"}}, {multi: true}); db.packages.find( {"duration":{"$gte":30},"asset_code":{"$in":["CH370","CH369","CH368"]},"streaming_mode" : "vodafone_android","msisdn":/VFPlay/,record_date:{"$gte": new Date('2018-06-05T00:00:00Z'),"$lte": new Date('2018-07-10T00:00:00Z')}} ) ====================================================================================================================================================================== db.ASSETS.update({"subject" : "epg"}, {$push: { "catlogue": "5ab0b35088abf255501c12f2" }},{multi:true}) db.UPLOADCRON.update({ "subject" : "epg" }, { $set: { "sync_status" : "pending" } }) ================================================================================================================================================================ db.packages.count({"streaming_mode" : "dailyhunt_digivive_offdeck",record_date:new Date('2018-07-03T00:00:00Z')}) db.packages.count({"streaming_mode" : "dailyhunt_digivive_offdeck",record_date:{"$gte": new Date('2018-07-03T00:00:00Z'),"$lte": new Date('2018-07-03T23:59:59Z')}}) db.free_paid_details.find({"app":"dailyhunt_digivive_offdeck",date:{"$gte": new Date('2018-07-04T00:00:00Z'),"$lte": new Date('2018-07-04T23:59:59Z')}}) db.packages.count({"streaming_mode" : "dailyhunt_digivive_offdeck",record_date:{"$gte": new Date('2018-07-03T00:00:00Z'),"$lte": new Date('2018-07-03T23:59:59Z')}}) db.packages.count({"streaming_mode" : "dailyhunt_digivive_offdeck",record_date:{"$gte": new Date('2018-07-03T00:00:00Z'),"$lte": new Date('2018-07-03T23:59:59Z')}}) db.free_paid_details.aggregate([{$match:{"app":"dailyhunt_digivive_offdeck",date:{"$gte": new Date('2018-06-18T00:00:00Z'),"$lte": new Date('2018-06-20T23:59:59Z')}}},{$group:{_id :total:{$sum:1},app:{$first:'$app'}}}]) db.free_paid_details.aggregate([{$match:{"app":"dailyhunt_digivive_offdeck",date:{"$gte": new Date('2018-06-01T00:00:00Z'),"$lte": new Date('2018-06-30T23:59:59Z')}}}]) db.nexgtv_app_hits_report.aggregate([{$match:{date:{"$gte": new Date('2018-06-18T00:00:00Z'),"$lte": new Date('2018-06-20T23:59:59Z')}}},{$group:{_id :{"date":"$date"}}}]) $group:{_id :{"tv_n":"$tv_n"} =============================================================================================================================================================== db.nexgtv_app_hits_report.aggregate([{$match:{date:{"$gte": new Date('2018-06-18T00:00:00Z'),"$lte": new Date('2018-06-20T23:59:59Z')}}},{$group:{_id :{"date":"$date"},totalcnt:{$sum:1},appname:{$first:'$appname'},totalcnt:{$first:'$totalcnt'},date:{$first:'$date'}}}]) //May -2021 db.packages.aggregate( [{$match:{"streaming_mode" : {'$ne':"vodafone_android"},asset_code:"CH903" ,"record_date":{'$gte': new Date('2020-12-01T00:00:00Z'), '$lte': new Date('2020-12-31T23:59:59Z')}}},{$group:{_id :{"asset_code": "$asset_code"},total_hits:{$sum:1}, ] ) db.packages.aggregate( [{$match:{"streaming_mode" : "dailyhunt_digivive_offdeck",asset_code:{$ne:'',$ne:null},record_date:{"$gte": new Date('2018-03-01T00:00:00Z'), "$lte": new Date('2018-03-04T23:59:59Z')}}},{$group:{_id :{"record_date": "$record_date" ,"asset_code":"$asset_code","paid_status":"$paid_status"},total_hits:{$sum:1},'duration':{'$sum':'$duration'}, duration:{$sum:'$duration'},record_date:{$first:'$record_date'}, asset_code:{$first:'$asset_code'},channel_content:{$first:'$channel_content'},broadcaster:{$first:'$broadcaster'},content_type:{$first:'$content_type'},language:{$first:'$language'},genre:{$first:'$genre'}}},{$sort:{'duration':-1}}] ) db.packages.aggregate( [{$match:{"streaming_mode" : {'$ne':"vodafone_android"},"duration":{"$gte":57},asset_code:"CH903" ,"record_date":{'$gte': new Date('2020-12-01T00:00:00Z'), '$lte': new Date('2020-12-31T23:59:59Z')}}},{$group:{_id :{"asset_code": "$asset_code","msisdn": "$msisdn"},total_hits:{$sum:1},"total_duration":{"$sum":"$duration"} ] ) "catalogue_name":"platform8" db.packages.aggregate( [{$match:{"catalogue_name" : "platform8",asset_code:"PFMOVASG01" ,"record_date":{'$gte': new Date('2021-09-06T00:00:00Z'), '$lte': new Date('2021-09-07T23:59:59Z')}}},{$group:{_id :{"asset_code": "$asset_code"},total_hits:{$sum:1},"total_duration":{"$sum":"$duration"} ] ) db.packages.aggregate([{"$match":{"record_date":{"$gte":{"$date":{"$numberLong":"1630953000000"}},"$lte":{"$date":{"$numberLong":"1631039399999"}}},"streaming_mode":{"$regex":"^platform8","$options":"i"}}},{"$group":{"_id":null,"totalHits":{"$sum":1},"total_duration":{"$sum":"$duration"}}},{"$project":{"_id":0,"totalHits":1,"total_duration":1}}]) db.packages.aggregate( [{$match:{"streaming_mode" : {'$ne':"vodafone_android"},"duration":{"$gte":57},asset_code:"CH903" ,"record_date":{'$gte': new Date('2020-12-01T00:00:00Z'), '$lte': new Date('2020-12-31T23:59:59Z')}}},{$group:{_id :{"asset_code": "$asset_code"},total_hits:{$sum:1}}} ] ) db.packages.aggregate( [{$match:{"streaming_mode" : {'$ne':"vodafone_android"},"duration":{"$gte":57},asset_code:"CH903" ,"record_date":{'$gte': new Date('2020-12-01T00:00:00Z'), '$lte': new Date('2020-12-31T23:59:59Z')}}},{$group:{_id :{"msisdn": "$msisdn"},total_hits:{$sum:1}}}, {$group:{_id :{"msisdn": "$_id.msisdn"},totalCount:{$sum:"$total_hits"},"distinctCount": { "$sum": 1 }}} ] ) db.packages.aggregate( [{$match:{"streaming_mode" : {'$ne':"vodafone_android"},"duration":{"$gte":57},asset_code:"CH902" ,"record_date":{'$gte': new Date('2020-12-01T00:00:00Z'), '$lte': new Date('2020-12-31T23:59:59Z')}}},{$group:{_id :{"msisdn": "$msisdn"},total_hits:{$sum:1}}}, {$group:{_id :{"msisdn": "$_id.msisdn"},totalCount:{$sum:"$total_hits"},"distinctCount": { "$sum": 1 }}} ] ) db.packages.aggregate( [{$match:{asset_code:{$ne:'',$ne:null},record_date:{"$gte": new Date('2018-03-01T00:00:00Z'), "$lte": new Date('2018-03-31T23:59:59Z')}}},{$group:{_id :{"record_date": "$record_date" ,"asset_code":"$asset_code","paid_status":"$paid_status"},total_hits:{$sum:1},'duration':{'$sum':'$duration'}, duration:{$sum:'$duration'},record_date:{$first:'$record_date'}, asset_code:{$first:'$asset_code'},channel_content:{$first:'$channel_content'},broadcaster:{$first:'$broadcaster'},content_type:{$first:'$content_type'},language:{$first:'$language'},genre:{$first:'$genre'}}},{$sort:{'duration':-1}}] ) db.KEYWORDS.aggregate( {$group : { '_id' : "$title",'status':{'$first':'$status'},'id':{'$first':'$_id'},"uniqueIds":{"$addToSet":"$_id"} }}, { $project : { title : 1,status:1,uniqueIds:1 } } ) db.KEYWORDS.aggregate( [ { $group : { _id : "$title", data: { $push: "$$ROOT" } } } ] ) db.ASSETS.update( { _id: 56e156139da23c60750e692d }, { $addToSet: { catlogue: "5d52a59dc8d1450aa73a8595" } } ) =========================================================================================================================================================== //Filter Records db.KEYWORDS.aggregate( [ { $match: { $text: { $search: "operating" } } }, { $sort: { score: { $meta: "textScore" }, posts: -1 } } ] ) ===================================================================================================================================================================== //Month Wise channel hits db.packages.aggregate( [{$match:{"content_type":"LIVE",streaming_mode:{$ne:'vodafone_android'},asset_code:{$ne:'',$ne:null},record_date:{"$gte": new Date('2021-08-01T00:00:00Z'), "$lte": new Date('2021-08-31T23:59:59Z')}}},{$group:{_id :{"asset_code":"$asset_code","paid_status":"$paid_status"},total_hits:{$sum:1},'duration':{'$sum':'$duration'}, duration:{$sum:'$duration'}, asset_code:{$first:'$asset_code'},channel_content:{$first:'$channel_content'},broadcaster:{$first:'$broadcaster'},content_type:{$first:'$content_type'},language:{$first:'$language'},genre:{$first:'$genre'}}},{$sort:{'duration':-1}}] ) ======================================================================================================================================================== $conver with on error /** * replacementDocument: A document or string. */ { "newRoot": { "$arrayToObject": [ [ { k: "$title", v: { '$arrayToObject': { '$concatArrays': [ [ { 'k': 'is_recurring', 'v': '$is_recurring' }, { 'k': '$title', 'v': { '$arrayToObject': { '$concatArrays': [ '$final_data' ] } } } ] ] } } } ] ] } } ============================================================================================================= [ { '$match': { 'parent_id': 0 } }, { '$sort': { '_id': 1 } }, { '$addFields': { 'parent_id': { '$toString': '$_id' } } }, { '$lookup': { 'from': 'PAYMENTMODES', 'localField': 'parent_id', 'foreignField': 'parent_id', 'as': 'data' } }, { '$addFields': { 'final_data': { '$map': { 'input': '$data', 'as': 'g', 'in': { 'k': '$$g.title', 'v': { 'is_recurring': '$$g.is_recurring', 'image': '$$g.image' } } } } } }, { '$replaceRoot': { 'newRoot': { '$arrayToObject': { '$concatArrays': [ [ { 'k': 'is_recurring', 'v': '$is_recurring' }, { 'k': 'image', 'v': { '$convert': { 'input': '$image', 'to': 'string', 'onNull': '', 'onError': '' } } }, { 'k': 'mode', 'v': '$title' }, { 'k': '$title', 'v': { '$arrayToObject': { '$concatArrays': [ '$final_data' ] } } } ] ] } } } } ] ============================================================================================================================================================ Key value root replace [ { '$match': { 'parent_id': 0 } }, { '$sort': { '_id': 1 } }, { '$addFields': { 'parent_id': { '$toString': '$_id' } } }, { '$lookup': { 'from': 'PAYMENTMODES', 'localField': 'parent_id', 'foreignField': 'parent_id', 'as': 'data' } }, { '$addFields': { 'final_data': { '$map': { 'input': '$data', 'as': 'g', 'in': { 'is_recurring': '$$g.is_recurring', 'image': '$$g.image', 'title': '$$g.title' } } } } }, { '$replaceRoot': { 'newRoot': { '$arrayToObject': { '$concatArrays': [ [ { 'k': 'is_recurring', 'v': '$is_recurring' }, { 'k': 'image', 'v': { '$convert': { 'input': '$image', 'to': 'string', 'onNull': '', 'onError': '' } } }, { 'k': 'mode', 'v': '$title' }, { 'k': '$title', 'v': '$final_data' } ] ] } } } } ] ========================================================================================================================================================== Filter with lookup [ { '$match': { 'type': 'movie' } }, { '$lookup': { 'from': 'BITFRAME', 'let': { 'code': '$code' }, 'pipeline': [ { '$match': { '$expr': { '$and': [ { '$eq': [ '$code', '$$code' ] } ] } } }, { '$sort': { 'bit_frame': 1 } } ], 'as': 'bitframeurl' } }, { '$addFields': { 'sd_download': { '$filter': { 'input': '$bitframeurl', 'as': 'num', 'cond': { '$and': [ { '$eq': [ '$$num.content_download', '1' ] }, { '$in': [ '$$num.bit_frame', [ '360', '320' ] ] } ] } } }, 'hd_download': { '$filter': { 'input': '$bitframeurl', 'as': 'num', 'cond': { '$and': [ { '$eq': [ '$$num.content_download', '1' ] }, { '$eq': [ '$$num.bit_frame', '720' ] } ] } } }, 'content': { '$filter': { 'input': '$bitframeurl', 'as': 'num', 'cond': { '$and': [ { '$eq': [ '$$num.content_download', '0' ] } ] } } } } }, { '$addFields': { 'content_obj': { '$first': '$content' }, 'sd_obj': { '$first': '$sd_download' }, 'hd_obj': { '$first': '$hd_download' } } }, { '$project': { '_id': 1, 'code': 1, 'premium_hls': '$content_obj.url', 'hd_url': '$hd_obj.url', 'sd_url': '$sd_obj.url', 'hd_bitframe': '$hd_obj.bit_frame', 'sd_bitframe': '$sd_obj.bit_frame', 'hd_size': 1, 'sd_size': 1 } } ] ==================================================================================================================================================== Update with foreach ["VOD81600"].forEach(function(result) { db.ASSETS.update({"code" : result}, {$set : {"viewerrating_id" : "6053008864dee72bd4e52ebe","content_advisory" : [ "605993c9e1e5ed93d4d36af9" ]}}); }) ==================================================================================================================================================== //BitFrame URL query [ { '$match': { 'type': 'movie', 'code': 'MOV4022' } }, { '$lookup': { 'from': 'BITFRAME', 'let': { 'code': '$code' }, 'pipeline': [ { '$match': { '$expr': { '$and': [ { '$eq': [ '$code', '$$code' ] } ] } } }, { '$sort': { 'bit_frame': 1 } } ], 'as': 'bitframeurl' } }, { '$addFields': { 'sd_download': { '$filter': { 'input': '$bitframeurl', 'as': 'num', 'cond': { '$and': [ { '$eq': [ '$$num.content_download', '1' ] }, { '$in': [ '$$num.bit_frame', [ '360', '320' ] ] } ] } } }, 'hd_download': { '$filter': { 'input': '$bitframeurl', 'as': 'num', 'cond': { '$and': [ { '$eq': [ '$$num.content_download', '1' ] }, { '$eq': [ '$$num.bit_frame', '720' ] } ] } } }, 'content': { '$filter': { 'input': '$bitframeurl', 'as': 'num', 'cond': { '$and': [ { '$eq': [ '$$num.content_download', '0' ] } ] } } } } }, { '$addFields': { 'content_obj': { '$first': '$content' }, 'sd_obj': { '$first': '$sd_download' }, 'hd_obj': { '$first': '$hd_download' } } }, { '$addFields': { 'bitframeurl_play': { 'premiumn_hls': { '$convert': { 'input': { '$first': '$content.url' }, 'to': 'string', 'onNull': '', 'onError': '' } }, 'standred_hls': '' }, 'hd': { 'url': { '$convert': { 'input': { '$first': '$hd_download.url' }, 'to': 'string', 'onNull': '', 'onError': '' } }, 'bitframe': { '$convert': { 'input': { '$first': '$hd_download.bit_frame' }, 'to': 'string', 'onNull': '', 'onError': '' } }, 'size': { '$convert': { 'input': '$hd_size', 'to': 'int', 'onNull': 0, 'onError': 0 } } }, 'sd': { 'url': { '$convert': { 'input': { '$first': '$sd_download.url' }, 'to': 'string', 'onNull': '', 'onError': '' } }, 'bitframe': { '$convert': { 'input': { '$first': '$sd_download.bit_frame' }, 'to': 'string', 'onNull': '', 'onError': '' } }, 'size': { '$convert': { 'input': '$sd_size', 'to': 'int', 'onNull': 0, 'onError': 0 } } } } }, { '$project': { '_id': 0, 'bitframeurl_play': 1, 'hd': 1, 'sd': 1, 'code': 1 } } ] ==================================================================================================================================================

No comments:

Post a Comment