如何在mongoose nodejs中更新嵌套文档?

huangapple go评论67阅读模式
英文:

How to update nested documents in mongoose nodejs?

问题

嗨,我在使用 mongoose 更新嵌套文档时遇到了困难。对于某种原因,当我尝试指定要更新的 flashcard 时,编辑器会抛出错误。有谁知道为什么会这样?

英文:

Hey guys I am struggling with updating a nested document w mongoose

flashcardDeck.updateOne({ _id: deck._id }, { $set: { flashcards[Math.floor(i/2)].side1: newFlashcardsArr[i]}});

for some reason the editor throws errors when I try to specify which flashcard to update.
Does anyone know why this is happening?

答案1

得分: 0

const idx = Math.floor(i/2);
flashcardDeck.updateOne({ _id: deck._id }, { $set: { `flashcards.${idx}.side1`: newFlashcardsArr[i]}});
英文:

Try with:

const idx = Math.floor(i/2);
flashcardDeck.updateOne({ _id: deck._id }, { $set: { `flashcards.${idx}.side1`: newFlashcardsArr[i]}});

huangapple
  • 本文由 发表于 2023年5月30日 05:42:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76360470.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定