问题
我有一个要更新的嵌套数组,但是当我执行findOneAndUpdate时,猫鼬只会为数组中的新对象创建一个对象ID,而不会传入我要保存的数据
下面是我的所有代码,非常感谢您的帮助,以帮助我摆脱困境!
代码
猫鼬模型
const instantSchema = mongoose.Schema({
userId: {type: mongoose.Schema.Types.ObjectId, ref: 'User', require: true},
name: {type: String, require: true},
startDate: {type: Date, require: true},
endDate: {type: Date, require: false},
length: {type: Number, require: true},
inProgress: {type: Boolean, require: true, default: true},
comps: [iCC]
});
const iCC = {
userId: {type: mongoose.Schema.Types.ObjectId, ref: 'User', require: true},
inProgress: {type: Boolean, require: true, default: true},
starting: {type: statsFieldsSchema, require: true},
dates: [{
statsFields: {type: datesSchema, require: true}
}],
rank: {type: Number, require: false},
finalDates: {type: datesSchema, require: false}
};
const datesSchema = {
days: {type: Number, require: true},
weeks: {type: Number, require: true},
months: {type: Number, require: true},
years: {type: Number, require: true}
}
执行findOneAndUpdate的代码
const newDates = {
days: 2,
weeks: 3,
months: 5,
years: 3
};
const newSave = await InstantComp.findOneAndUpdate(
{
_id: instantId,
'comps.userId': user.userId
},
{$push: {'comps.$.stats': newDates}},
{new: true}
);