• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

117
Views
how to remove an object inside the array of objects in mongodb

I want to remove an object inside the array of objects and I am doing this

router.post("/delVendAttach", async (req, res) => {
  try {
    let vend = await Vendors.findOneAndUpdate({ "level1.email": req.body.email }, {
      $pull: {
        "level2.attachments": {
          _id: req.body.id
        }
      }
    })
    return res.status(200).send("Attachment Deleted Successfully");
  } catch (error) {
    console.log("error", error);
    return res.status(400).send(error);
  }
});

Here is the img of db collection

about 3 years ago · Juan Pablo Isaza
2 answers
Answer question

0

I think you find from level1.email and remove from level2 that the problem. Try below code may be works for you !

let vend = await Vendors.findOneAndUpdate({ "level1.email": req.body.email }, {
  $pull: {
    "level1.attachments": {
      _id: req.body.id
    }
  }
})
about 3 years ago · Juan Pablo Isaza Report

0

Duplicate of Using $pull in Mongodb to remove a deeply embedded object

about 3 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 Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error