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

123
Views
Facing some issue when trying to delete object from array - mongodb

I am trying to delete object from an array. If the object value match that object will be deleted from the database. Here is my database example:

[{
        _id: "621773f39ec6fe3a3728d686",
        label: "size",
        slug: "size",
        vendor: "admin",
        options: [
            {
                label: "M",
                value: "M"
            },
            {
                label: "XXL",
                value: "XXL"
            },
            {
                label: "XL",
                value: "XL"
            },
            {
                label: "S",
                value: "S"
            }
        ]
    }
    ]

This is my code:

        // DELETE FILED FROM ATTRIBUTE
        app.put('/dashboard/attribute/fieldDelete/:id', async (req, res) => {
            const id = req.params.id
            const { value } = req.body
            unityMartAttributes.updateOne({ value: value }, { $pull: { options: { _id: objectId(id) } } })

        })

Expected output: value: "M" matched that's why that object removed

[{
        _id: "621773f39ec6fe3a3728d686",
        label: "size",
        slug: "size",
        vendor: "admin",
        options: [

            {
                label: "XXL",
                value: "XXL"
            },
            {
                label: "XL",
                value: "XL"
            },
            {
                label: "S",
                value: "S"
            }
        ]
    }
    ]
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can do it like this:

app.put('/dashboard/attribute/fieldDelete/:id', async (req, res) => {
  const id = req.params.id
  const { value } = req.body

  await unityMartAttributes.updateOne({ "_id": id }, {
    "$pull": {
      "options": {
        "value": value
      }
    }
  });

}

Working example

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!