0

I have a document like this:

[ { _id: 55535305c6d17454276beba1,
    name: 'Localhost',
    tables: [{ chairs:[{
                         data:[{
                         }]
                      }]
            }] 
  }
]

I want to update/insert new data in the data document.

2
  • 1
    Deep nested document is not a good idea. Your document structure will not make your life easier. You definitely going to have head explosion Commented May 13, 2015 at 14:05
  • i cant change this structure and yes i have a head explosion. Commented May 13, 2015 at 14:07

1 Answer 1

0

You can do something along these lines for your example:

db.collectionName.update(
    {_id: 55535305c6d17454276beba1}, 
    {$set: {"tables.chairs.data" : [{somethingHere:1}] }}
);

You can update individual elements in the data array by using this syntax:

db.collectionName.update(
    {_id: 55535305c6d17454276beba1}, 
    {$set: {"tables.chairs.data.0" : {somethingHere:2} }}
);

This link provides some additional examples: Updating Value of Array Element in MongoDB

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.