Database

[{"_id":ObjectId("62bc081b7cf3d332fb1cc60c"),"saleNumber":"U16564900119581015","salesOffice":"6003f66466081f1a21412659","salesOfficeName":"MO02-CT Concierge Service Unify SAR","taproFileId":null,"headers":{"x-credit-customer-id":"0001500012","x-booking-channel":"faceToFace","CLIENT_IP":"10.110.239.128","X-Real-IP":"10.110.239.128","X-Forwarded-For":"10.4.0.22","X-Forwarded-Proto":"https","x-host":"hub-api","host":"hub-api","X-Elb-IP":"","user-agent":"admin@seera.sa","x-loyalty-program-code":"ALM_SA"},"carts":[{"id":"cart-4a5e0626-433d-4d93-92c8-b1a9b75b1ae5","type":"hotel","createdAt":"2022-06-29 01:06:51","storeId":1021.0}],"contact":{"title":"Mr","firstName":"Abdullah","lastName":"DSSD","email":"FFDFD@TEST.COM","vatRegId":null,"countryCode":"+966","phone":"155555555"},"status":"New","storeId":"1015","currency":"SAR","isDeleted":false,"salesOfficeCode":"MO02","caseId":"122222","invoiceGenerated":true,"updatedAt":ISODate("2022-06-29T13:10:36.659Z"),"createdAt":ISODate("2022-06-29T08:06:51.264Z"),"payments":[{"paymentId":"62bc4e7de9a25e5c652f29a7","paymentType":"charge","paymentCurrency":"SAR","paymentMode":"cash","paymentTotal":13501.88,},{"paymentId":"62bc4f116d0b273f414129e7","paymentType":"refund","paymentCurrency":"SAR","paymentMode":"","paymentTotal":13061.879999999997,}],"orders":[{"orderId":"62bc4ea2ee9b0d54e7486e17","orderNumber":"H22062900471021","orderCartId":"cart-4a5e0626-433d-4d93-92c8-b1a9b75b1ae5","orderType":"hotel","orderCategory":"hotel","orderTotal":13501.8799}]},{"_id":ObjectId("62bc125b606f5b7c2d7899ac"),"saleNumber":"U16564926351771000","salesOffice":"5d8a2e4d949764f32e021d8d","salesOfficeName":"WR01-CT JED Concept Store","taproFileId":null,"headers":{"x-user-id":"62a338c3db55ea0010588502","x-user-account-status":"active","x-booking-channel":"phone","CLIENT_IP":"10.110.111.67","X-Real-IP":"10.110.111.67","X-Forwarded-For":"10.110.111.67","X-Forwarded-Proto":"http","x-host":"hub-unify-api.tajawal-dev.internal","host":"hub-unify-api.tajawal-dev.internal","X-Elb-IP":"","user-agent":"hub-admin@seera.sa","x-loyalty-program-code":"ALM_SA"},"carts":[{"id":"cart-b655e614-5d11-4df1-b4f9-d23cb3932c24","type":"hotel","createdAt":"2022-06-29 11:59:25","storeId":1021.0}],"contact":{"title":"Mr","firstName":"Sagar","lastName":"Sreejith","email":"sreejith.sagar@gmail.com","vatRegId":null,"countryCode":"+971","phone":"568961022"},"status":"New","storeId":"1000","currency":"SAR","isDeleted":false,"salesOfficeCode":"WR01","invoiceGenerated":true,"updatedAt":ISODate("2022-06-29T12:25:55.468Z"),"createdAt":ISODate("2022-06-29T08:50:35.201Z"),"payments":[{"paymentId":"62bc3eaedbb3e3396557f8d6","paymentType":"charge","paymentCurrency":"SAR","paymentMode":"cash","paymentTotal":6341.58,"penalties":0.0},{"paymentId":"62bc44cafc907a21516d93d5","paymentType":"refund","paymentCurrency":"SAR","paymentMode":"","paymentTotal":40.0,"penalties":41.0}],"orders":[{"orderId":"62bc3ed961c56f0a1c7ec156","orderNumber":"H22062900441021","orderCartId":"cart-b655e614-5d11-4df1-b4f9-d23cb3932c24","orderType":"hotel","orderCategory":"hotel","orderTotal":6341.58}]},{"_id":ObjectId("62bc154b7b4b1359fb39c68e"),"saleNumber":"U16564933879081015","salesOffice":"6003f66466081f1a21412659","salesOfficeName":"MO02-CT Concierge Service Unify SAR","taproFileId":null,"headers":{"x-credit-customer-id":"0001500012","x-booking-channel":"phone","CLIENT_IP":"10.110.111.67","X-Real-IP":"10.110.111.67","X-Forwarded-For":"10.110.111.67","X-Forwarded-Proto":"http","x-host":"hub-unify-api.tajawal-dev.internal","host":"hub-unify-api.tajawal-dev.internal","X-Elb-IP":"","user-agent":"hub-admin@seera.sa","x-loyalty-program-code":"ALM_SA"},"carts":[{"id":"cart-d8c4539f-664d-4162-af39-2120545df855","type":"flight","createdAt":"2022-06-29 01:17:24","storeId":1021.0}],"contact":{"title":"Mr","firstName":"Sagar","lastName":"Sreejith","email":"sreejith.sagar@gmail.com","vatRegId":null,"countryCode":"+966","phone":"568961022"},"status":"New","storeId":"1015","currency":"SAR","isDeleted":false,"salesOfficeCode":"MO02","invoiceGenerated":true,"updatedAt":ISODate("2022-06-29T13:19:16.129Z"),"createdAt":ISODate("2022-06-29T09:03:07.158Z"),"payments":[{"paymentId":"62bc50f8811db72ed74c4676","paymentType":"charge","paymentCurrency":"SAR","paymentMode":"cash","paymentTotal":747.5,"penalties":0.0},{"paymentId":"62bc51417c81a17d3c6dcbfc","paymentType":"refund","paymentCurrency":"SAR","paymentMode":"","paymentTotal":547.5,"penalties":200.0}],"orders":[{"orderId":"62bc5109b5999457ae7059d7","orderNumber":"A22062900511021","orderCartId":"cart-d8c4539f-664d-4162-af39-2120545df855","orderType":"flight","orderCategory":"flight","orderTotal":747.5}]},{"_id":ObjectId("62bc372ceadcb96f2e2df76c"),"saleNumber":"U16565020608701000","salesOffice":"5da54c6c949764ee830957f4","salesOfficeName":"CR01-CT Riyadh Al Malqa branch","taproFileId":null,"headers":{"x-user-id":"61ed057a0d0fe475d9167c62","x-user-account-status":"active","x-booking-channel":"faceToFace","CLIENT_IP":"10.110.162.184","X-Real-IP":"10.110.162.184","X-Forwarded-For":"10.55.100.54","X-Forwarded-Proto":"https","x-host":"hub-api","host":"hub-api","X-Elb-IP":"","user-agent":"nedaa.albouq@seera.sa","x-loyalty-program-code":"ALM_SA"},"carts":[{"id":"cart-5d348c84-3dfd-420e-b9f6-7a7894690df2","type":"flight","createdAt":"2022-06-29 11:38:08","storeId":1000.0},{"id":"cart-eea1e24b-0969-4c9c-88bf-6f220c277606","type":"hotel","createdAt":"2022-06-30 12:00:59","storeId":1000.0},{"id":"cart-09caf87c-8ccd-4ded-a221-c6ca4f9fe2d5","type":"hotel","createdAt":"2022-06-30 12:16:53","storeId":1000.0}],"contact":{"title":"Mrs","firstName":"Nedaa","lastName":"Albouq","email":"Nedaa.albouq@seera.sa","vatRegId":null,"countryCode":"+966","phone":"591179229"},"status":"New","storeId":"1000","currency":"SAR","isDeleted":false,"salesOfficeCode":"CR01","invoiceGenerated":true,"updatedAt":ISODate("2022-06-30T15:20:17.107Z"),"createdAt":ISODate("2022-06-29T11:27:40.686Z"),"payments":[{"paymentId":"62bc3a74c38ab00380474fc9","paymentType":"charge","paymentCurrency":"SAR","paymentMode":"qitaf","paymentTotal":1300.0,"penalties":0.0},{"paymentId":"62bc3b45dbb3e3396557f8ca","paymentType":"charge","paymentCurrency":"SAR","paymentMode":"cash","paymentTotal":1391.0,"penalties":0.0}],"orders":[{"orderId":"62bc3bc507fd337f09306652","orderNumber":"A22062900411000","orderCartId":"cart-5d348c84-3dfd-420e-b9f6-7a7894690df2","orderType":"flight","orderCategory":"flight","orderTotal":2690.76},{"orderId":"62bd92af300b36492f7ff8a5","orderNumber":"H22063000441000","orderCartId":"cart-eea1e24b-0969-4c9c-88bf-6f220c277606","orderType":"hotel","orderCategory":"hotel","orderTotal":29505.28},{"orderId":"62bd943ca931da140477b58b","orderNumber":"H22063000451000","orderCartId":"cart-09caf87c-8ccd-4ded-a221-c6ca4f9fe2d5","orderType":"hotel","orderCategory":"hotel","orderTotal":763.94}]},{"_id":ObjectId("62bc3d54d62a3461ba36251f"),"saleNumber":"U165650363645852","salesOffice":"5811e17ee5631810146a2c92","salesOfficeName":"Almosafer iOS SA","headers":{"x-user-id":"12345","x-loyalty-program-code":"ALM_SA","x-booking-channel":"online","CLIENT_IP":"10.110.71.185","X-Real-IP":"10.110.71.185","X-Forwarded-For":"10.110.71.185","X-Forwarded-Proto":"http","x-host":"hub-api-dev-endpoint.tajawal.com","host":"hub-api-dev-endpoint.tajawal.com","X-Elb-IP":"","user-agent":"system"},"carts":[{"id":"cart-fabb3bc6-ccd9-4731-8784-b168d6640cf7","type":"accommodation","storeId":52.0,"createdAt":"2022-06-29 11:53:56"}],"contact":{"title":"Mr","firstName":"Abdul","lastName":"Hafeez","email":"abdul.hafeez@seera.sa","countryCode":"+966","phone":"971453323231"},"status":"New","storeId":"52","currency":"SAR","isDeleted":false,"invoiceGenerated":false,"updatedAt":ISODate("2022-06-29T11:53:56.825Z"),"createdAt":ISODate("2022-06-29T11:53:56.746Z"),"orders":[{"orderId":"62bc3d3361c56f0a1c7ec150","orderNumber":"C220629004252","orderCartId":"cart-fabb3bc6-ccd9-4731-8784-b168d6640cf7","orderType":"accommodation","orderCategory":"accommodation","orderTotal":800.0}],"payments":[{"paymentId":"62bc3d54d62a3461ba362520","paymentType":"charge","paymentCurrency":"SAR","paymentMode":"online","paymentTotal":800.0,"penalties":0.0}]},{"_id":ObjectId("62bc4802bca6a34e184c8846"),"saleNumber":"U16565063701271000","salesOffice":"5d8a2e4d949764f32e021d8d","salesOfficeName":"WR01-CT JED Concept Store","taproFileId":null,"headers":{"x-user-id":"61d45e2049123847ef1d5542","x-user-account-status":"active","x-booking-channel":"faceToFace","CLIENT_IP":"10.110.162.184","X-Real-IP":"10.110.162.184","X-Forwarded-For":"10.72.52.13","X-Forwarded-Proto":"https","x-host":"hub-api","host":"hub-api","X-Elb-IP":"","user-agent":"sindhu.vijayendra@seera.sa","x-loyalty-program-code":"ALM_SA"},"carts":[{"id":"cart-3a602055-eb7f-4364-829b-58ed6fc3bfef","type":"accommodation","createdAt":"2022-06-29 12:42:52","storeId":1000.0},{"id":"cart-e0c652b8-790d-4dfb-8b31-113c4138fe7f","type":"flight","createdAt":"2022-06-29 01:04:40","storeId":1000.0}],"contact":{"title":"Mr","firstName":"Hub","lastName":"Staging","email":"seera.hub.stg@gmail.com","vatRegId":null,"countryCode":"+966","phone":"9916647781"},"status":"New","storeId":"1000","currency":"SAR","isDeleted":false,"salesOfficeCode":"WR01","caseId":"81273123","invoiceGenerated":true,"updatedAt":ISODate("2022-06-29T16:10:47.169Z"),"createdAt":ISODate("2022-06-29T12:39:30.963Z"),"payments":[{"paymentId":"62bc4975bc60386b1a695119","paymentType":"charge","paymentCurrency":"SAR","paymentMode":"cash","paymentTotal":2000.0,"penalties":0.0},{"paymentId":"62bc4dfb811db72ed74c4655","paymentType":"charge","paymentCurrency":"SAR","paymentMode":"cash","paymentTotal":2745.75,"penalties":0.0}],"orders":[{"orderId":"62bc4985ee9b0d54e7486e02","orderNumber":"C22062900451000","orderCartId":"cart-3a602055-eb7f-4364-829b-58ed6fc3bfef","orderType":"accommodation","orderCategory":"accommodation","orderTotal":2000.0},{"orderId":"62bc4e0907fd337f09306658","orderNumber":"A22062900461000","orderCartId":"cart-e0c652b8-790d-4dfb-8b31-113c4138fe7f","orderType":"flight","orderCategory":"flight","orderTotal":2745.75}]},{"_id":ObjectId("62bc4ebe6d0b273f414129e5"),"saleNumber":"U16565080943611000","salesOffice":"5d8a2e4d949764f32e021d8d","salesOfficeName":"WR01-CT JED Concept Store","taproFileId":null,"headers":{"x-user-id":"5efb106ff228f33db30b8612","x-user-account-status":"active","x-booking-channel":"faceToFace","CLIENT_IP":"10.110.162.184","X-Real-IP":"10.110.162.184","X-Forwarded-For":"10.72.104.214","X-Forwarded-Proto":"https","x-host":"hub-api","host":"hub-api","X-Elb-IP":"","user-agent":"hub-admin@seera.sa","x-loyalty-program-code":"ALM_SA"},"carts":[{"id":"cart-41adad79-f136-42a6-9412-5beff2ab4459","type":"activity","createdAt":"2022-06-29 01:09:25","storeId":1021.0}],"contact":{"title":"Mr","firstName":"Abdullah","lastName":"Rahim","email":"abdullah@gmail.com","vatRegId":null,"countryCode":"+971","phone":"589648055"},"status":"New","storeId":"1000","currency":"SAR","isDeleted":false,"salesOfficeCode":"WR01","caseId":"12345","invoiceGenerated":true,"updatedAt":ISODate("2022-06-29T13:14:35.625Z"),"createdAt":ISODate("2022-06-29T13:08:14.246Z"),"payments":[{"paymentId":"62bc4f146d0b273f414129e9","paymentType":"charge","paymentCurrency":"SAR","paymentMode":"cash","paymentTotal":170.0,"penalties":0.0}],"orders":[{"orderId":"62bc4f1cee9b0d54e7486e26","orderNumber":"S22062900481021","orderCartId":"cart-41adad79-f136-42a6-9412-5beff2ab4459","orderType":"activity","orderCategory":"activity","orderTotal":123.0}]},{"_id":ObjectId("62bc4fbbf8002b5d3e68565a"),"saleNumber":"U165650834750352","salesOffice":"5811e17ee5631810146a2c92","salesOfficeName":"Almosafer iOS SA","headers":{"x-user-id":"12345","x-loyalty-program-code":"ALM_SA","x-booking-channel":"online","CLIENT_IP":"10.110.201.52","X-Real-IP":"10.110.201.52","X-Forwarded-For":"10.110.201.52","X-Forwarded-Proto":"http","x-host":"hub-api-dev-endpoint.tajawal.com","host":"hub-api-dev-endpoint.tajawal.com","X-Elb-IP":"","user-agent":"system"},"carts":[{"id":"cart-7d4bcdc6-4fba-44ed-9004-3c000d4beb7c","type":"accommodation","storeId":52.0,"createdAt":"2022-06-29 01:12:27"}],"contact":{"title":"Mr","firstName":"Abdul","lastName":"Hafeez","email":"abdul.hafeez@seera.sa","countryCode":"+966","phone":"971453323231"},"status":"New","storeId":"52","currency":"SAR","isDeleted":false,"invoiceGenerated":false,"updatedAt":ISODate("2022-06-29T13:12:27.307Z"),"createdAt":ISODate("2022-06-29T13:12:27.202Z"),"orders":[{"orderId":"62bc4f9bb5999457ae7059d5","orderNumber":"C220629004952","orderCartId":"cart-7d4bcdc6-4fba-44ed-9004-3c000d4beb7c","orderType":"accommodation","orderCategory":"accommodation","orderTotal":2500.0}],"payments":[{"paymentId":"62bc4fbbf8002b5d3e68565b","paymentType":"charge","paymentCurrency":"SAR","paymentMode":"online","paymentTotal":2500.0,"penalties":0.0}]}]

Query

db.collection.aggregate([{$match:{"_id":ObjectId("62bc081b7cf3d332fb1cc60c")}},{$project:{grandTotal:{$reduce:{input:"$payments",initialValue:0,in:{$cond:[{$eq:["$$this.paymentType","charge"]},{$add:["$$this.paymentTotal","$$value"]},{$subtract:[{$add:[{$ifNull:["$$this.penalties",0]},"$$this.paymentTotal"]},"$$value"]}]}}}}}])

Result