0
\$\begingroup\$

I have three mongodb collections. I would like to group the collections by date, month and year, then sum by condition in order to display totals from the three collections in one table. However, i have a problem with generating the mongodb aggregation code.

collectionA

{
  _id: "1",
  "Name": "John",
  "Booked": "Executive"
  "date": 2023 - 03 - 02 T18: 15: 26.723 + 00: 00
}, {
  _id: "2",
  "Name": "Peter",
  "Booked": "Economy",
  "date": 2023 - 03 - 19 T19: 15: 26.723 + 00: 00
}, {
  _id: "3",
  "Name": "Wade",
  "Booked": "Economy",
  "date": 2023 - 03 - 19 T19: 15: 26.723 + 00: 00
},

collectionB

{
  _id: "1",
  "Name": "John",
  "Amount": 200,
  "date": 2023 - 03 - 19 T19: 15: 26.723 + 00: 00
}, {
  _id: "2",
  "Name": "Peter",
  "Amount": 300,
  "date": 2023 - 03 - 19 T19: 15: 26.723 + 00: 00
}, {
  _id: "3",
  "Name": "Wade",
  "Amount": 100,
  "date": 2023 - 03 - 19 T19: 15: 26.723 + 00: 00
},

collectionC

{
  _id: "3",
  "Name": "Wade",
  "Country": "USA",
  "date": 2023 - 03 - 19 T19: 15: 26.723 + 00: 00
},

I would like to be able to sum by date, month and year total Economy bookings, also be able to sum by date, month and year the country of origin for customers.

How can i improve this code

collectionA.aggregate([
              {$lookup:
                {
                  from: "collectionB",
                  localField: "_id",
                  foreignField: "_id",
                  as: "collectionB"
                }
              },
              {$unwind: "$collectionB"},
              {$lookup:
                {
                   from: "collectionC",
                   localField: "_id",
                   foreignField: "_id",
                   as: "collectionC"
                }
              },
              {$unwind: "$collectionC"},
              {
                "$group": {
                  _id: {
                    year: {$year: "$date"},
                    month: {$month: "$date"},
                    dayOfMonth: {$dayOfMonth: "$date"},
                  },
                  "Economy": {
                    $sum: {
                      "$cond": [
                        {"$eq": ["$Booked", "Economy"]},
                        1,
                        0
                      ]
                    }
                  },
                  "USA": {
                    $sum: {
                      "$cond": [
                        {"$eq": ["$Country", "USA"]},
                        1,
                        0
                      ]
                    }
      
                  }
                }
              }
         ])

Thanks in advance

\$\endgroup\$
1
  • \$\begingroup\$ Welcome to Code Review! The description states "I would like to be able to sum by date, month and year total Economy bookings, also be able to sum by date, month and year the country of origin for customers.". Does the code currently handle this? \$\endgroup\$ Mar 20 at 23:28

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.