因此,我用子模式(allocationAndLocationSchema)创建了以下模型。每个MovementSchema中都将包含许多嵌入式的distributionAndLocationSchemas。我需要更新单个的distributionAndLocationSchemas并将其保存到模型中。
我尝试在子模式上进行一次forEach,但是更新似乎不起作用。是否有一种简单的解决方案来按id(createdByUserID)搜索模型,并按userID搜索子模式,然后更新其纬度和经度?
模型和子模式:
var allocationAndLocationSchema = new Schema({
roleID: String,
roleTitle: String,
userID: String,
latitude: String,
longitude: String,
lastUpdated: Date
});
var MovementSchema = new Schema({
dateCreated: Date,
createdByUserID: String,
dateEdited: Date,
allocationAndLocation: [allocationAndLocationSchema]
});
答案 0 :(得分:0)
它只是原始的,因此,请从中吸取灵感。
MovementSchema.findByIdAndUpdate({createdByUserID:req.body.createdByUserID,'allocationAndLocation.userID':req.body.userID},{$ set:{allocationAndLocation。$。longitude:req.body.longitude,allocationAndLocation。$。纬度:req.body.latitude},function(err,res){ //回调函数 }