Home > Mobile >  Summarize Child Items in Json Object
Summarize Child Items in Json Object

Time:01-24

I have 2 Objects like this :

[
  {
    _id: ObjectId("62990f96345ef9001d9f2dfe"),
    deletedAt: null,
    expiredAt: ISODate("2022-06-05T19:29:26.746Z"),
    dataBarang: [
      {
        vendor: ObjectId("6215dd91139c99003fe4c7cd"),
        currency: ObjectId("61f750b56ea409001e521043"),
        modelId: 14,
        usdConvertion: 1,
        price: 15,
        leadTime: null,
        quantity: 2
      }
    ],
    cartItems: [
      ObjectId("62990f77345ef9001d9f2dfd")
    ],
    reqNo: "f0124ae0-1072-4f18-ab90-a7d2d784e634",
    reqDate: ISODate("2022-06-03T00:00:00.000Z"),
    updatedAt: ISODate("2022-06-02T19:29:30.140Z"),
    createdAt: ISODate("2022-06-02T19:29:26.747Z"),
    __v: 0,
    pickUpDate: ISODate("2022-06-03T00:00:00.000Z"),
    transactionDate: ISODate("2022-06-03T00:00:00.000Z")
  },
  {
    _id: ObjectId("62990fe9345ef9001d9f2e03"),
    deletedAt: null,
    expiredAt: ISODate("2022-06-05T19:30:49.081Z"),
    dataBarang: [
      {
        vendor: ObjectId("6215dd91139c99003fe4de15"),
        currency: ObjectId("61f750b56ea409001e521043"),
        modelId: 14,
        usdConvertion: 1,
        price: 64.82,
        leadTime: 14,
        quantity: 1
      }
    ],
    cartItems: [
      ObjectId("62990fcc345ef9001d9f2e02")
    ],
    reqNo: "c2b378ad-87fd-4db4-96b8-6812cb1f2229",
    reqDate: ISODate("2022-06-03T00:00:00.000Z"),
    updatedAt: ISODate("2022-06-02T19:30:51.269Z"),
    createdAt: ISODate("2022-06-02T19:30:49.082Z"),
    __v: 0,
    pickUpDate: ISODate("2022-06-03T00:00:00.000Z"),
    transactionDate: ISODate("2022-06-03T00:00:00.000Z")
  }
]

And what I want to achieve is, to Sum the price of dataBarang for each Object every transactionDate. Please take note that each Objects able to have multiple dataBarang.

I tried with my latest code but still not showing something :

    this.myDBservices.aggregate([])
          .match({
            transactionDate: {
              $gte: '2023-01-01',
              $lte: '2023-01-20'
            }
          })
          .group({
            _id: { $dateFromParts:{year:{$year:"$transactionDate"}, month:{$month:"$transactionDate"}, day:{$dayOfMonth : "$transactionDate" }} },
            date: { $first: "$transactionDate" },
            total: { $sum: 1 },
            totalPrice : {
              $map: {
                $sum: "$$dataBarang.quantity"
              }
            }
          });

Maybe someone want to help me what should It be? really appreciate for every help. Thank you

CodePudding user response:

There are multiple problems with your current attempt.

  1. your schema's date fields are probably in date type, but in your first $match you are matching with string. This will result with no document matched.
  2. dataBarang.quantity is in an array. You will need 2 `$sum to correctly sum the field
  3. There are many typos / syntax errors, which I will leave it back to you to debug.

Below is a simple demo on how you should $sum the dataBarang.quantity field:

db.collection.aggregate([
  {
    $match: {
      transactionDate: {
        $gte: ISODate("2021-01-01"),
        $lte: ISODate("2023-01-20")
      }
    }
  },
  {
    $group: {
      _id: {
        $dateTrunc: {
          date: "$transactionDate",
          unit: "day"
        }
      },
      totalPrice: {
        $sum: {
          $sum: "$dataBarang.price"
        }
      }
    }
  }
])

Mongo Playground

  • Related