MongoDB Chart $multiply

78 Views Asked by At

I have a data like this.

"products": [{
        "_id": {
            "$oid": "5ffd0a8f6273740017cc5fca"
        },
        "name": "Banana",
        "price": 65,
        "createdAt": {
            "$date": "2021-01-12T02:33:51.648Z"
        },
        "updatedAt": {
            "$date": "2021-01-12T02:33:51.648Z"
        },
        "quantity": 3
    }, {
        "_id": {
            "$oid": "5ffd09326273740017cc5fb3"
        },
        "name": "Apple",
        "price": 79,
        "createdAt": {
            "$date": "2021-01-12T02:28:02.412Z"
        },
        "updatedAt": {
            "$date": "2021-01-12T02:28:02.412Z"
        },
        "quantity": 2
    }]

What I'm trying to do is multiply the price over the quantity.

Implementation:

{ $reduce: {
  input: '$products', initialValue: 0,
  in: { $multiply: ["$products.price",
      "$products.quantity"] } } 

I'm having an error $multiply only supports numeric types, not array

0

There are 0 best solutions below