英文:
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]}});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论