我有这个模式
const topicSchema = new mongoose.Schema({
topic: {
type: String,
required: true
},
imageUri: {
type: String,
required: true,
default: "https://picsum.photos100/id/200/100/"
},
likes: {
type: Number,
default: 0
},
likedBy: {
type: [String]
},
comments: {
type: [String]
},
dateCreated: {
type: Date,
default: Date.now
}
});
const PSupportGroup = mongoose.model(
"PSupportGroup",
new mongoose.Schema({
groupName: {
type: String,
required: true
},
adminName: {
type: String,
required: true
},
adminImageUri: {
type: String,
required: true
},
groupDetails: {
type: String,
required: true
},
dateCreated: {
type: Date,
default: Date.now
},
topics: [topicSchema]
})
);
我要像这样更新嵌入文档中的顶赞和顶赞属性:
let groups = await PSupportGroup.findOne(
{ _id: req.params.gid },
{
topics: { $elemMatch: { _id: req.params.tid } }
}
);
groups.topics[0].likedBy.push(req.params.id);
groups.topics[0].likes++;
groups.save();
尝试保存时出现此错误:
'DivergentArrayError:为了您自己的利益,使用'document.save()'更新使用$ elemMatch投影选择的数组,或者在操作时使用跳过,限制,查询条件或排除_id字段进行填充结果不支持整个数组的$ pop或$ set。以下路径可能会被不安全地修改:','topics.0.likes','使用Model.update()来更新这些数组。'
请帮助。