因此,我创建了以下带有子模式(allocationAndLocationSchema)的模型。每个MovementSchema都会有许多嵌入的allocationAndLocationSchemas。我需要更新单个allocationAndLocationSchemas并将它们保存到模型中。
我尝试对子模式执行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]
});
发布于 2018-10-10 16:07:39
它只是原始的,所以,从它的想法。
函数'allocationAndLocation.userID':req.body.userID},{$set:{allocationAndLocation.$.longitude:req.body.longitude,allocationAndLocation.$.latitude : req.body.latitude},MovementSchema.findByIdAndUpdate({createdByUserID:req.body.createdByUserID,(err,res){ //回调函数}
https://stackoverflow.com/questions/52733929
复制相似问题