Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

234
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!