0

i have the following collection, I have one question about:

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

how i can update all price concurrently, (price=50 in all objects) thanks for help

1
  • Which language or connector are you using Commented Oct 16, 2021 at 23:12

1 Answer 1

1

You can use positional all operator to update all elements in the array

db.collectionName.update(
   { },
   { $set: { "items.$[].price" : 50 } },
   { multi: true }
)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.