J
J
Jelay2017-06-01 17:48:58
JavaScript
Jelay, 2017-06-01 17:48:58

MongoDB how to update nested objects?

there is a database structure:

todolists[
      {
       name: "Books",
       id: ObjectID(заданный Mongo),
       needs:[
              {
               task: "Harry Potter1",
               isCompleted: false
              },
              {
              task: "Harry Potter2",
               isCompleted: true
              }
              ]
       },
       {
       name: "Places",
       id: ObjectID(заданный Mongo),
       needs:[
              {
               task: "Boodapesht",
               isCompleted: false
              },
              {
              task: "Varshava",
               isCompleted: true
              }
              ]
       }

And you need to change the value of the isCompleted field in task-a with the name "Varshava", in todolist-e with the name "Places" to false.
How can I reach him so deeply? The following code added the necessary task objects to todolists. (Maybe it will help somehow)
db.get().collection('todolists').findOneAndUpdate({_id: ObjectID(activeID)}, 
{ $push: {                                                                                     
    needs: {                                                                                  
      task: todo.task,                                                                                    
      isCompleted: todo.isCompleted                                                                                   
}                                                                               
}                                                                           
}, {new: true}
...

Help me please.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
longclaps, 2017-06-01
@Jelay

db.todolists.findOneAndUpdate(
    {"needs.task": "Varshava"},
    {$set: {"needs.$.isCompleted": false}}
);

A
Anton Anton, 2017-06-01
@Fragster

You that, a relational DB from it want to make? Change the entire object.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question