Database

db={"contracts":[{"_id":ObjectId("630c9e23fb146c4b3b801b9e"),"detention_id":[],"route_exclude":false,"trip_count":null,"fill_first":false,"is_dedicated":false,"truck_count":0,"running_trip_count":0,"is_round_trip_eligible":false,"is_synced":false,"bdr_label":"","bdr_charge":0,"is_migrated":false,"current_trip_count":0,"client_id":ObjectId("5e47c1f4a697c60195050059"),"updated_by":ObjectId("60a39adad079f9004d144cb8"),"user_id":ObjectId("60a39adad079f9004d144cb8"),"transporter_id":ObjectId("61d6d2227465c501d0915794"),"vehicle_type_id":ObjectId("61e1b3ad00516e004c893145"),"detention_group":"","delivery_type":2,"source":"amaravathi","destination":"Kozhikode","isDirect":false,"charge_level_key":["Test","chennai"],"distance":100,"status":1,"contract_type":1,"current":{"minimum_rate":0,"multiplying_factor":null,"min_slab":0,"additional_charge_percentage":0,"hilly_region_addition_percentage":0,"return_freight_rate":0,"surge_threshold":0,"fixed_rate":null,"oda_rate":null,"oda_unit":3,"minimum_oda":0,"point_charges":0,"pickup_point_charges":0,"start":ISODate("2022-08-26T00:00:00.000+05:30"),"end":ISODate("2023-08-24T23:59:00.000+05:30"),"freight_unit":1,"freight_rate":5000,"transit_time":172800000},"next":[],"previous":[],"lookup_key":"f93187d8364fca313ab92c83ac39eafa","created_at":ISODate("2022-08-26T15:26:15.865+05:30"),"updated_at":ISODate("2022-08-29T13:20:39.058+05:30")}],"depots":[{"_id":ObjectId("6304804a9f90b8018844fc20"),"consignee_ref_ids":[],"status":1,"coordinates":[12.9039441,80.14567129999999],"unloading_capacity":0,"has_pit_stop":false,"type":1,"plant_type":1,"ports_associated":[],"overseas_customer":false,"sez":false,"timezone":"Asia/Kolkata","fax_number":"","is_optima_group_by_cluster":"","country":"India","name":"CTB","reference_id":"CTB","client_id":ObjectId("5e47c1f4a697c60195050059"),"address":"CTB","city":"Chennai","state":"Tamil Nadu","region":"South","pincode":"600126","phone":"","delivery_type":1,"short_code":"CTB","gstn_no":"HSKK309393k","gates":[{"coordinates":[12.9039441,80.14567129999999],"exclude_trucks":[],"_id":ObjectId("630480789f90b8018844fc21"),"country":"India","name":"CTG1","reference_id":"CTG1","status":1,"address":"CTB","city":"Chennai","state":"Tamil Nadu","region":"South","pincode":"600126","short_code":"CTG1","special_tax_rebate":false,"gstin":null,"is_virtual_gate":false,"category":"","delivery_do_not_combine":false,"combine":false,"loading_time":{"start":null,"end":null},"unloading_time":{"start":null,"end":null},"legal_entity":null,"display_reference_name":"","updated_at":ISODate("2022-08-23T12:53:46.233+05:30"),"created_at":ISODate("2022-08-23T12:53:36.046+05:30")}],"docks":[],"updated_by":ObjectId("5eaba6438a4895004916adbe"),"display_reference_name":"","receivers":[],"created_at":ISODate("2022-08-23T12:52:50.182+05:30"),"updated_at":ISODate("2022-08-23T13:09:18.468+05:30")},{"_id":ObjectId("62fb93a711ea3f019ffe19e6"),"consignee_ref_ids":[],"status":1,"coordinates":[20.593684,78.96288],"unloading_capacity":0,"has_pit_stop":false,"type":1,"plant_type":1,"ports_associated":[],"overseas_customer":false,"sez":false,"timezone":"Asia/Kolkata","fax_number":"","is_optima_group_by_cluster":"","country":"India","name":"Test","reference_id":"Test","client_id":ObjectId("5e47c1f4a697c60195050059"),"address":"test","city":"Test","state":"Test","region":"North","pincode":"0100301","phone":"","delivery_type":1,"short_code":"test","gstn_no":"1111","gates":[],"docks":[],"updated_by":ObjectId("62e7b04d91a9b50189b4319d"),"display_reference_name":"","receivers":[],"created_at":ISODate("2022-08-16T18:25:03.453+05:30"),"updated_at":ISODate("2022-08-16T18:25:03.913+05:30")},{"_id":ObjectId("630cc2ba38f87401899c35f1"),"consignee_ref_ids":[],"status":1,"coordinates":[20.593684,78.96288],"unloading_capacity":0,"has_pit_stop":false,"type":1,"plant_type":1,"ports_associated":[],"overseas_customer":false,"sez":false,"timezone":"Asia/Kolkata","fax_number":"","is_optima_group_by_cluster":"","country":"India","name":"B20220829191440","reference_id":"B20220829191440","client_id":ObjectId("62d8f30ce763689b942d5e12"),"address":"No 15,GST Street","city":"Blr20220829191440","state":"Karnataka","region":"South","pincode":"123456","phone":"5588","delivery_type":1,"short_code":"B214945","gstn_no":"123","gates":[{"coordinates":[10.9290666,78.7438114],"exclude_trucks":[],"_id":ObjectId("630cc2bb38f87401899c35f2"),"country":"India","name":"Gate 2","reference_id":"BG20220829191440","status":1,"address":"No 15 GST Road","city":"Blr20220829191440","state":"Karnataka","region":"South","pincode":"123456","short_code":"G1214945","special_tax_rebate":false,"is_virtual_gate":false,"category":"b","combine":true,"updated_at":ISODate("2022-08-29T19:14:27.912+05:30"),"created_at":ISODate("2022-08-29T19:14:27.551+05:30")}],"docks":[],"updated_by":ObjectId("62d8f30ce763689b942d5e15"),"display_reference_name":"","receivers":[],"created_at":ISODate("2022-08-29T19:14:26.892+05:30"),"updated_at":ISODate("2022-08-29T19:14:27.912+05:30")},{"_id":ObjectId("630cbf98c9b7c401971c7554"),"consignee_ref_ids":[],"status":1,"coordinates":[20.593684,78.96288],"unloading_capacity":0,"has_pit_stop":false,"type":1,"plant_type":1,"ports_associated":[],"overseas_customer":false,"sez":false,"timezone":"Asia/Kolkata","fax_number":"","is_optima_group_by_cluster":"","country":"India","name":"D-2","reference_id":"D-2","client_id":ObjectId("5cc01648d97fec4e37b0cd76"),"address":"rose","city":"Chennai","state":"Tamilnadu","region":"South","pincode":"145786","phone":"","delivery_type":1,"short_code":"D-2","gstn_no":"GSTIN1234576431s","gates":[{"coordinates":[20.593684,78.96288],"exclude_trucks":[],"_id":ObjectId("630cbfcac9b7c401971c7555"),"country":"India","name":"G-21","reference_id":"G-21","status":1,"address":"rose","city":"Chennai","state":"Tamilnadu","region":"South","pincode":"145786","short_code":"G-21","special_tax_rebate":false,"gstin":null,"is_virtual_gate":false,"category":"","delivery_do_not_combine":false,"combine":false,"loading_time":{"start":null,"end":null},"unloading_time":{"start":null,"end":null},"legal_entity":null,"display_reference_name":"","updated_at":ISODate("2022-08-29T19:01:54.902+05:30"),"created_at":ISODate("2022-08-29T19:01:54.576+05:30")}],"docks":[],"updated_by":ObjectId("630769f6f0e23401ad24f0d5"),"display_reference_name":"","receivers":[],"created_at":ISODate("2022-08-29T19:01:04.449+05:30"),"updated_at":ISODate("2022-08-29T19:01:54.902+05:30")},{"_id":ObjectId("630cbf25c9b7c401971c7552"),"consignee_ref_ids":[],"status":1,"coordinates":[20.593684,78.96288],"unloading_capacity":0,"has_pit_stop":false,"type":1,"plant_type":1,"ports_associated":[],"overseas_customer":false,"sez":false,"timezone":"Asia/Kolkata","fax_number":"","is_optima_group_by_cluster":"","country":"India","name":"D-1","reference_id":"D-1","client_id":ObjectId("5cc01648d97fec4e37b0cd76"),"address":"rose","city":"Chennai","state":"Tamilnadu","region":"South","pincode":"486259","phone":"","delivery_type":1,"short_code":"D-1","gstn_no":"GstIn123456TY09","gates":[{"coordinates":[20.593684,78.96288],"exclude_trucks":[],"_id":ObjectId("630cbf52c9b7c401971c7553"),"country":"India","name":"G-11","reference_id":"G-11","status":1,"address":"rose","city":"Chennai","state":"Tamilnadu","region":"South","pincode":"486259","short_code":"G-11","special_tax_rebate":false,"gstin":null,"is_virtual_gate":false,"category":"","delivery_do_not_combine":false,"combine":false,"loading_time":{"start":null,"end":null},"unloading_time":{"start":null,"end":null},"legal_entity":null,"display_reference_name":"","updated_at":ISODate("2022-08-29T18:59:54.647+05:30"),"created_at":ISODate("2022-08-29T18:59:54.352+05:30")}],"docks":[],"updated_by":ObjectId("630769f6f0e23401ad24f0d5"),"display_reference_name":"","receivers":[],"created_at":ISODate("2022-08-29T18:59:09.238+05:30"),"updated_at":ISODate("2022-08-29T18:59:54.647+05:30")},{"_id":ObjectId("630cbf0338f87401899c35ef"),"consignee_ref_ids":[],"status":1,"coordinates":[20.593684,78.96288],"unloading_capacity":0,"has_pit_stop":false,"type":1,"plant_type":1,"ports_associated":[],"overseas_customer":false,"sez":false,"timezone":"Asia/Kolkata","fax_number":"","is_optima_group_by_cluster":"","country":"India","name":"B20220829185855","reference_id":"B20220829185855","client_id":ObjectId("62d8f30ce763689b942d5e12"),"address":"No 15,GST Street","city":"Blr20220829185855","state":"Karnataka","region":"South","pincode":"123456","phone":"8097","delivery_type":1,"short_code":"B641556","gstn_no":"123","gates":[{"coordinates":[10.9290666,78.7438114],"exclude_trucks":[],"_id":ObjectId("630cbf0438f87401899c35f0"),"country":"India","name":"Gate 2","reference_id":"BG20220829185855","status":1,"address":"No 15 GST Road","city":"Blr20220829185855","state":"Karnataka","region":"South","pincode":"123456","short_code":"G1641556","special_tax_rebate":false,"is_virtual_gate":false,"category":"b","combine":true,"updated_at":ISODate("2022-08-29T18:58:36.974+05:30"),"created_at":ISODate("2022-08-29T18:58:36.665+05:30")}],"docks":[],"updated_by":ObjectId("62d8f30ce763689b942d5e15"),"display_reference_name":"","receivers":[],"created_at":ISODate("2022-08-29T18:58:35.994+05:30"),"updated_at":ISODate("2022-08-29T18:58:36.974+05:30")},{"_id":ObjectId("630cbdb938f87401899c35ed"),"consignee_ref_ids":[],"status":1,"coordinates":[20.593684,78.96288],"unloading_capacity":0,"has_pit_stop":false,"type":1,"plant_type":1,"ports_associated":[],"overseas_customer":false,"sez":false,"timezone":"Asia/Kolkata","fax_number":"","is_optima_group_by_cluster":"","country":"India","name":"B20220829185386","reference_id":"B20220829185386","client_id":ObjectId("62d8f30ce763689b942d5e12"),"address":"No 15,GST Street","city":"Blr20220829185386","state":"Karnataka","region":"South","pincode":"123456","phone":"9939","delivery_type":1,"short_code":"B985134","gstn_no":"123","gates":[{"coordinates":[10.9290666,78.7438114],"exclude_trucks":[],"_id":ObjectId("630cbdba38f87401899c35ee"),"country":"India","name":"Gate 2","reference_id":"BG20220829185386","status":1,"address":"No 15 GST Road","city":"Blr20220829185386","state":"Karnataka","region":"South","pincode":"123456","short_code":"G1985134","special_tax_rebate":false,"is_virtual_gate":false,"category":"b","combine":true,"updated_at":ISODate("2022-08-29T18:53:06.732+05:30"),"created_at":ISODate("2022-08-29T18:53:06.465+05:30")}],"docks":[],"updated_by":ObjectId("62d8f30ce763689b942d5e15"),"display_reference_name":"","receivers":[],"created_at":ISODate("2022-08-29T18:53:05.869+05:30"),"updated_at":ISODate("2022-08-29T18:53:06.732+05:30")},{"_id":ObjectId("630cbd0338f87401899c35eb"),"consignee_ref_ids":[],"status":1,"coordinates":[20.593684,78.96288],"unloading_capacity":0,"has_pit_stop":false,"type":1,"plant_type":1,"ports_associated":[],"overseas_customer":false,"sez":false,"timezone":"Asia/Kolkata","fax_number":"","is_optima_group_by_cluster":"","country":"India","name":"B20220829185042","reference_id":"B20220829185042","client_id":ObjectId("62d8f30ce763689b942d5e12"),"address":"No 15,GST Street","city":"Blr20220829185042","state":"Karnataka","region":"South","pincode":"123456","phone":"5908","delivery_type":1,"short_code":"B637721","gstn_no":"123","gates":[{"coordinates":[10.9290666,78.7438114],"exclude_trucks":[],"_id":ObjectId("630cbd0438f87401899c35ec"),"country":"India","name":"Gate 2","reference_id":"BG20220829185042","status":1,"address":"No 15 GST Road","city":"Blr20220829185042","state":"Karnataka","region":"South","pincode":"123456","short_code":"G1637721","special_tax_rebate":false,"is_virtual_gate":false,"category":"b","combine":true,"updated_at":ISODate("2022-08-29T18:50:04.842+05:30"),"created_at":ISODate("2022-08-29T18:50:04.586+05:30")}],"docks":[],"updated_by":ObjectId("62d8f30ce763689b942d5e15"),"display_reference_name":"","receivers":[],"created_at":ISODate("2022-08-29T18:50:03.870+05:30"),"updated_at":ISODate("2022-08-29T18:50:04.842+05:30")},{"_id":ObjectId("630cbc5238f87401899c35e9"),"consignee_ref_ids":[],"status":1,"coordinates":[20.593684,78.96288],"unloading_capacity":0,"has_pit_stop":false,"type":1,"plant_type":1,"ports_associated":[],"overseas_customer":false,"sez":false,"timezone":"Asia/Kolkata","fax_number":"","is_optima_group_by_cluster":"","country":"India","name":"B20220829184704","reference_id":"B20220829184704","client_id":ObjectId("62d8f30ce763689b942d5e12"),"address":"No 15,GST Street","city":"Blr20220829184704","state":"Karnataka","region":"South","pincode":"123456","phone":"1951","delivery_type":1,"short_code":"B371744","gstn_no":"123","gates":[{"coordinates":[10.9290666,78.7438114],"exclude_trucks":[],"_id":ObjectId("630cbc5338f87401899c35ea"),"country":"India","name":"Gate 2","reference_id":"BG20220829184704","status":1,"address":"No 15 GST Road","city":"Blr20220829184704","state":"Karnataka","region":"South","pincode":"123456","short_code":"G1371744","special_tax_rebate":false,"is_virtual_gate":false,"category":"b","combine":true,"updated_at":ISODate("2022-08-29T18:47:07.707+05:30"),"created_at":ISODate("2022-08-29T18:47:07.412+05:30")}],"docks":[],"updated_by":ObjectId("62d8f30ce763689b942d5e15"),"display_reference_name":"","receivers":[],"created_at":ISODate("2022-08-29T18:47:06.726+05:30"),"updated_at":ISODate("2022-08-29T18:47:07.707+05:30")}],"contract_sub_types":[{"_id":ObjectId("6315a0187786245c3749e1de"),"client_id":ObjectId("5c6a810a0b36940b6cae25a7"),"contract_id":ObjectId("62eaac1b35eb9602acd5832b"),"charge_level_key":["Chennai","Banglore"],"charge_level":"routes","created_at":ISODate("2022-09-05T12:37:04.367+05:30"),"min_rate":null,"rate":163,"unit":3,"updated_at":ISODate("2022-09-05T12:37:04.367+05:30")},{"_id":ObjectId("6315a0187786245c3749e1df"),"client_id":ObjectId("5c6a810a0b36940b6cae25a7"),"contract_id":ObjectId("62eaac1b35eb9602acd5832b"),"charge_level_key":["Chennai","Salem"],"charge_level":"routes","created_at":ISODate("2022-09-05T12:37:04.367+05:30"),"min_rate":null,"rate":175,"unit":3,"updated_at":ISODate("2022-09-05T12:37:04.367+05:30")}]}

Query

db.contracts.aggregate([{$match:{_id:ObjectId("630c9e23fb146c4b3b801b9e")}},{$set:{charge_level_key:{$map:{input:"$charge_level_key",in:{"$toLower":"$$this"}}}}},{$lookup:{from:"depots",let:{charge_level_key:"$charge_level_key"},pipeline:[{$match:{$expr:{$in:[{$toLower:"$city"},"$$charge_level_key"]}}}],as:"depots"}},{$sort:{count:-1}}])

Result