3

Say I have a document like this:

{
    _id: kjakldjflkajda
    property: value
    nest: [
    {
       one: 1
       two: 2
    },
    {
       one: 2
       two: 3
    }
    ]
}

How do I update the nested one property from 2 to 3 without changing the other property from 1 to 3

0

1 Answer 1

2

Try this:

MyCollection.update({ _id: 'kjakldjflkajda' }, { $set: { 'nest.0.one': 3 } });
Sign up to request clarification or add additional context in comments.

3 Comments

There's a better way to do without hard coding the index to update. But the question has been asked many times before.
@NeilLunn Yes, it was probably asked before. I am totally aware of "other methods", but this one is the simplest and whether "other method" are "better" probably depends on the specific use case.
What If I want change all elements in the array? is it 'nest.$.one' ?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.