0

I want to update this document's subscribers array if property 'name' is the same as the name I specify, if not add it to the array

{
    id: "Test-123456789",
    subscribers: [
        {
            name: "Test1",
            points: 123
        },
        {
            name: "Test2",
            points: 3282
        }

    ]
}

1 Answer 1

1

You can use positional operator $ like this:

db.collection.update({
  "id": "Test-123456789",
  "subscribers.name": yourValue
},
{
  "$set": {
    "subscribers.$.points": 0
  }
})

Example here.

Note that I've modified points field but using $ you can access the object and modify as you want, like this example

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.