1
我有此架構:貓鼬推動在陣列中的子文檔嵌套
var UserSchema = new Schema({
profile: {
firstName: {type: String, trim: true, required: true },
lastName: {type: String, trim: true, required: true },
city: {type: String}
age: {type: Number, min: 13, max: 120},
image: {type: String,trim: true}
},
friends:{
accepted:[{
id: {type: mongoose.Schema.Types.ObjectId, ref: 'User'},
dateAccepted: {type: Date, default: Date.now}
}]
}
}
如何可以與.update方法friends.accepted推送數據?
我只能用findOne方法成功:
User.findOne({
_id: id
},function(err,user){
user.friends.accepted.push(newUser._id);
user.save(function(err){if(err) return err;})
});
我找到了答案,更新,這是 _id:newUser._id 即使在我的模型中它的ID –