node.js - 按 id 查找但更新子模式
问题描述
所以我用子模式(allocationAndLocationSchema)创建了以下模型。每个 MovementSchema 中都会有许多嵌入的 allocationAndLocationSchema。我需要更新各个 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]
});
解决方案
它只是生的,所以,从中汲取灵感。
MovementSchema.findByIdAndUpdate({createdByUserID:req.body.createdByUserID,'allocationAndLocation.userID': req.body.userID},{$set:{allocationAndLocation.$.longitude:req.body.longitude ,allocationAndLocation.$.latitude : req .body.latitude},function(err,res){ //回调函数 }
推荐阅读
- vuejs2 - Tornado 和 Vue 模板冲突
- c# - 通过 Xamarin 连接到 Web 服务
- php - PHP中具有多个条件的if-else
- c# - 如何使用 NPOI 在 docx 文档的开头插入表格
- html - 如何使用 CSS 在表单周围添加边框?
- c++ - BEGIN_MESSAGE_MAP 错误“非静态成员引用必须相对于特定对象”MFC
- css - Internet Explorer IE 上 Font Awesome 面临的问题
- xcode - 使用 Picker View 时 UITest 中断
- firebase - google-services.json 文件中存储了什么样的数据(请详细讨论)
- php - 如何在 PHP 的 eval() 中将条件答案分配给变量?