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

239
Vistas
How to remove some array in mongodb with nodejs?

I have the data like this

{
  "_id": "61794cfbbe84b5f1f8ae4e16",
  "image": "www.imagetest.com",
  "title": "test pembuatan artikel",
  "category": [
    "informasi",
    "budidaya"
  ],
  "description": "tes artikel",
  "createdAt": "2021-10-27T12:58:35.969Z",
  "updatedAt": "2021-11-06T01:49:06.012Z",
  "__v": 0
}

i want to remove some category from category arrays by selected _id. I have tried $pull but it does not change in my database. I've tried with $pull update method like this:

deleteCategory: async (req, res, next) => {
  const { articleId } = req.params
  const { category } = req.body.category
  try {
    await Article.updateOne(
      {_id: articleId},
      { $pull: {category: category}}
    )
    res.json({
      message: 'Article deleted successfully!',
    })
  } catch (error) {
    res.status(500).json({
      message: 'An error Occured!',
    })
  }
}

with this route:

router.delete('/category/delete/:articleId', ArticleController.deleteCategory)

Anyone knows what i'm missing?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

i dont know what just happen, but i tried this and it works. I'm pretty sure i've tried this before and it's not working, but i tried this again and it works

const { articleId } = req.params
    
try {
  await Article.updateOne(
    {_id: articleId},
    { $pull: {category: req.body.category}}
  )
  res.json({
    message: 'Article deleted successfully!',
  })
} catch (error) {
  res.status(500).json({
    message: 'An error Occured!',
  })
}

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