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

253
Vistas
MongooseJS: How to remove 1 object inside Array in Array of Objects

Hi MongooseJS experts!

I'm new in MongooseJS, This is my 2nd day of solving this problem but I can't find a working solution to this.

Thank you in advance!

My Delete method

Cart.updateOne(
    { "content.merchantId": req.body.merchantId },
    { $pull: { "content.items._id": req.body.productId } },
    { new: true },
    function (error, result) {
      if (error) { }
      else if (result) { }
    }
);

Schema

const CartSchema = new Schema({
      customerId: {
        type: String,
        required: true,
      },
      content: {
        merchantName: {
          type: String,
          required: true,
        },
        merchantId: {
          type: String,
          required: true,
        },
        items: [],
      },
});

Sample JSON

[
    {
        "content": {
            "merchantName": "SAMPLE_MERCHANT_NAME",
            "merchantId": "SAMPLE_MERCHANT_ID",
            "items": [
                {
                    "_id": "SAMPLE_ID",
                    "title": "SAMPLE TITLE",
                }
            ]
        },
        "_id": "618220e83966345ab5d451cd",
        "__v": 0
    },
]

Error message

Cannot use the part (_id) of (content.items._id) to traverse the element ({items: [{ _id: "SAMPLE_ID", title: "SAMPLE TITLE"}] })
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

you should use like this

db.collection.update({
  "content.merchantId": "SAMPLE_MERCHANT_ID"
},
{
  $pull: {
    "content.items": {
      "_id": "SAMPLE_ID"
    }
  }
},
{
  new:true
},
)

https://mongoplayground.net/p/Ou26tab2mBU

about 4 years ago · Juan Pablo Isaza 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