Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

517
Vistas
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 !

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

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

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda