我是蒙哥的新手。我需要在MongoDB模型中更新一个嵌套字段。这是我的代码:-
const employee = await empModel.searchData(Query);
countryArray.push({lang: 'eng', result 100});
countryArray.push({lang: 'german', result 99});
employee[0].country = countryArray;
employee[0].markModified('country');
employee[0].save();
empModel的架构:
onst mongoSchema = new Schema({
empId: [{
type: Schema.Types.ObjectId
}],
country:[{
lang:String,
result:Number
}],
projectId: {
type: String,
required: true
}
});
答案 0 :(得分:0)
使用简单的更新查询代替查找和更新
static async updateData(query, countryArray) {
const updateData= await this.update(query, { $set: { country: countryArray }});
console.log(updated successfully')
return 'Updated successfully'
}
const countryArray = []
countryArray.push({lang: 'eng', result 100})
countryArray.push({lang: 'german', result 99})
const update = await empModel.updateData(Query, countryArray)