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

Will Hua
  • 1,729
  • 2
  • 23
  • 33

1 Answers1

2

Try this:

MyCollection.update({ _id: 'kjakldjflkajda' }, { $set: { 'nest.0.one': 3 } });
Tomasz Lenarcik
  • 4,762
  • 17
  • 30
  • There's a better way to do without hard coding the index to update. But the question has been asked many times before. – Neil Lunn Mar 13 '15 at 06:01
  • @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. – Tomasz Lenarcik Mar 13 '15 at 06:04
  • What If I want change all elements in the array? is it `'nest.$.one'` ? – Gereltod Sep 22 '15 at 10:21