【发布时间】:2022-01-15 09:47:12
【问题描述】:
我有这样的计划。使用命令时,我希望在那里添加新项目
const userSchema = new mongoose.Schema({
_id: { //user id
type: String,
required: true,
},
books: [{ //book that user uses
_id: { //book id
type: Number,
required: true,
},
lessons: [{ //lessons of the book with progress
type: String,
required: true,
}],
}],
})
我正在尝试以这种方式添加它们。
await userSchema.findByIdAndUpdate(author.id, { $addToSet: { books: {_id: bookid, lessons: progress} } })
bookid - int 值加一。
progress - 字符串数组
但是只有一组课程被写入基础,没有 id。 当我只添加 id 时,不会添加 id,而是添加一系列课程。 我已经花了几个小时,我不明白为什么它不想将所有东西都添加到数组中。请帮忙。
【问题讨论】:
标签: node.js mongodb mongoose discord.js mongoose-schema