猫鼬,更改数组中的值

问题描述 投票:0回答:1

我正在尝试更新数组中的值,我尝试过这种方式:Mongoose, update values in array of objects

但是它似乎对我不起作用:

let myLessonDB = await myLessonSchema.findById(req.params.id);

myLessonDB.update(
    { 'lessons.lesson': req.body.lessonID },
    { $set: { 'lessons.$.present': true } }
);

myLessonDB返回此:

{"_id":"5eafff00726616772ca852e2",
"lessons":[{"level":"1","present":false,"lesson":"5eb00211154ac86dc8459d6f"}],
"__v":0}

我正在尝试通过所示的课程ID更改课程中的值,但是它不起作用。没有错误或看起来无法在数组中找到对象的任何东西

有人知道我在做什么错吗?

mongodb mongoose
1个回答
0
投票
let myLessonDB = await myLessonSchema.findById(req.params.id);

myLessonDB.lessons.map(oneLes => {
  if(oneLes.lesson == req.body.lessonID){
    oneLes.present = true;
  }
})

myLessonDB.save().then( finalLesson => {
  console.log(finalLesson);
})
© www.soinside.com 2019 - 2024. All rights reserved.