slmartin
slmartin

Reputation: 51

Updating a deeply nested object in mongodb

I'm trying to update the favorite Boolean to true in the following model.

I only need a success/failure return, so I've been trying update with a projection. However, I can't seem to get any further than selecting articles, but am not able to change the favorite value given the title name.

I am using the mongoDB node.js driver

Thanks!

_id: id,
news: [
    { 
      name: bbc,
      articles: [
                  {
                    title: 'flashpoint', favorite: false
                  }
                ]
    }
]

Upvotes: 1

Views: 356

Answers (1)

Nicolas Ducom
Nicolas Ducom

Reputation: 248

You're actually hitting one of the limitations of MongoDB, a Ticket on their Jira was opened in 2010 regarding updating items in deeply nested arrays, and has just been implemented, stated for release with Mongo 3.5.12. If you're using an older version, the simplest solution would be to go for a second collection containing your articles (with references on the first collection), so you can update them directly.

Upvotes: 2

Related Questions