我有這樣的方案。使用命令時,我希望在那里添加新專案
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 值加 1。
progress- 字串陣列
但是只有一組課程被寫入基地,沒有id。當我只添加 id 時,不會添加 id,而是添加一系列課程。我已經花了幾個小時,我不明白為什么它不想將所有內容添加到陣列中。請幫忙。
uj5u.com熱心網友回復:
嘗試與$push運營商:
await userSchema.findByIdAndUpdate(author.id, {
$push: {
books: {
_id: bookid,
lessons: progress
}
}
})
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/378984.html
