Indrapreet
Indrapreet

Reputation: 55

Mongoose - How to increment a field by value of another field

I am new to mongoose ODM. I am trying to increment a value by the value of another field. Following is what I have tried :


const deletedCartItem = await Cart.findOneAndUpdate(
    { sessionId: req.session.sessionId, "productInfo.productId": product_id },
    {
      $inc: {
        checkoutAmount: "$productInfo.totalAmount"//totalAmount is a nested field here.
      },
      $pull: {
        productInfo: { productId: product_id },
      },
    },
    { new: true, useFindAndModify: false }
  );


The schema of my collection looks like :


const cartSchema = mongoose.Schema({
  sessionId: {
    type: String,
    unique: true,
  },

  checkoutAmount: {
    type: Number,
  },

  orderStatus: {
      type: String,
      default: "INCART"
  },

  productInfo: [
    {
      productId: {
        type: String,
        required: true,
        unique: true,
      },
      quantity: {
        type: Number,
        required: true,
      },
      price: {
        type: Number,
      },
      totalAmount: {
        type: Number,
      }
    }
  ],
});

The error I am getting is - enter image description here

Am I missing something here ? Thanks in advance !

Upvotes: 1

Views: 594

Answers (1)

turivishal
turivishal

Reputation: 36104

Update can't allow to use internal field's value into another fields, you have to go with update with aggregation pipeline starting from MongoDB 4.2,

  • $reduce to iterate look of productInfo and check condition if productId match then return its totalAmount,
  • $add to increment the checkoutAmount with returned totalAmount from above $reduce operation
  • $filter to iterate loop of productInfo and filter the elements that do not have matching productId
const deletedCartItem = await Cart.findOneAndUpdate(
  { sessionId: req.session.sessionId, "productInfo.productId": product_id },
  [{
    $set: {
      checkoutAmount: {
        $add: [
          "$checkoutAmount",
          {
            $reduce: {
              input: "$productInfo",
              initialValue: 0,
              in: {
                $cond: [
                  { $eq: ["$$this.productId", product_id] },
                  "$$this.totalAmount",
                  "$$value"
                ]
              }
            }
          }
        ]
      },
      productInfo: {
        $filter: {
          input: "$productInfo",
          cond: { $ne: ["$$this.productId", product_id] }
        }
      }
    }
  }],
  { new: true, useFindAndModify: false }
);

Playground

Upvotes: 2

Related Questions