[Vuejs]-Update mongoDB with todolist information?

0👍

When a new todo is saved after clicking plus button, make a request on /updateTodoList with parent document’s _id. So your request might look something like this:

POST /updateTodoList:

Body:

{ "_id": "5caca1498accb128c8974d56",
  "todo": {
     "description": "This is a newly added todo description",
     "completed": false
   }
}

Then, on the server side, parse this body object and update the document with matching _id and push the new todo into the document. Your query will look something like this:

todolist.findOneAndUpdate({_id: req.body._id}, { $push: {todos: req.body.todo } })

Hope this helps.

Edit:

Each time you push a todo using above query, mongo inserts that element to the todos array. For pushing multiple todos in single query, use $each operator along with $push. Official documentation here.

Leave a comment