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

260
Vistas
Remove Object from array in Mongoose

I'm having this problem when removing an Object inside Array. I don't seems to understand what is the proper way of removing an item using $pull. Thanks in advance!

JSON data

"accountId": "62efd8c008f424af397b415d",
"links": [
    {
        "url": "www.github.com/iluvkohii",
        "title": "GitHub"
    },
    {
        "url": "www.github.com/iluvkohii",
        "title": "GitHub"
    },
    {
        "url": "www.github.com/iluvkohii",
        "title": "GitHub",
        "_id": "62efe1ee4675d0baa10bd219"
    },
    {
        "url": "www.github.com/iluvkohii",
        "title": "GitHub",
        "linkId": "62efe4e142099faeeb5999d0"
    }
]

Code

  try {
    const { accountId, _id } = req.body;
    return Profile.findOneAndUpdate(
      { accountId },
      { $pull: { links: { _id } } },
      { new: true, }
    )
      .then((value) => res.status(200).json(value))
      .catch((err) => res.status(400).json(err.errors));
  } catch (error) {
    console.error(error);
  }
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I just solved my own question after a day realizing that a normal String is not equal to Mongoose ObjectId

  try {
    const { accountId, _id } = req.body;
    return Profile.findOneAndUpdate(
      { accountId },
      { $pull: { links: { id: mongoose.Types.ObjectId(_id) } } },
      { new: true },
    )
      .then((value) => res.status(200).json(value))
      .catch((err) => res.status(400).json(err.errors));
  } catch (error) {
    console.error(error);
  }
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