how to save() DB.find({}) all models returned with an method in mongoose

I want query to DB and edit some value in the returned Query/rows. when we have a Query/row we can do it with model.save() but when we have an array of Query,how we can do it? some thing like this:

let arr=await DB_cat.find({parentId:1})
arr[0].name='first'
arr[1].name='sec'
arr.save()

Answer

you can use for of like this:

  let arr=await DB_cat.find({parentId:1})
  arr[0].name='first'
  arr[1].name='sec'
  for (let item of arr){
    await item.save()
  }

Leave a Reply

Your email address will not be published. Required fields are marked *