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

124
Views
How to update specific item of object in array of mongdb document

Assume we have the following collection, which I have few questions about:

{
    "_id" : ObjectId("4faaba123412d654fe83hg876"),
    "user_id" : 123456,
    "total" : 100,
    "items" : [
            {
                    "item_name" : "my_item_one",
                    "price" : 20
            },
            {
                    "item_name" : "my_item_two",
                    "price" : 50
            },
            {
                    "item_name" : "my_item_three",
                    "price" : 30
            }
    ]
}

In this case, I have to update item_name OR price at once or maybe Both at the same time.

For Example: If I provide item_name in req.body so it should only update item_name and price remain the same but if I provide price then it should do vice versa.

Basically, I mean to say it should only update that field which is provided in req.body.

I tried this

const result = await User.updateOne(
  {
    user_id: 123456,
    'items.item_name': 'my_item_two',
  },
  { $set: { items: req.body } }
)

But this is doing the same thing as I wanted but it is removing the remaining fields which are already there!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I think you can create the $set object in JS and then pass it to the query like this:

// body is to mock req.body
const body_1 = {
    item_name: "new_name",
    price: 1
}
const body_2 = {
    item_name: "new_name_2"
}
const body_3 = {
    price: 2
}

const getSetObject = (body) => {
    update = {
        $set: {}
    }
    Object.keys(body).forEach(k => {
        update.$set[`items.$.${k}`] = body[k]
    })
    return update
}

console.log(getSetObject(body_1))
console.log(getSetObject(body_2))
console.log(getSetObject(body_3))

Note how this create the objects used in these queries:

  • Two values
  • One value (item_name)
  • One value (price)

So you can create the query:

const result = await User.updateOne(
  {
    user_id: 123456,
    'items.item_name': 'my_item_two',
  },
  setObj
)
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!