【发布时间】:2018-06-21 06:16:00
【问题描述】:
就目前而言,我可以使用 Mongo shell 使该功能正常工作,但使用 Mongoose 在 Node.js 内的嵌套数组存在问题。
成功的Mongo shell命令:
db.test.update({}, {$set: {"this.$[i].that.$[x].amnt": 10000}}, {arrayFilters: [{"i._id": ObjectId('5a568059bc44142a9ca33d09')}, {"x.userId": ObjectId('5a466acb864c752f8c9890c6')}]})
结果:将 amnt 字段更改为 10000。在对从数据库中提取的给定字符串调用 ObjectId('') 之前,没有返回匹配项。
Node.js 代码:
var conditions = {}
var update = {$set: {"this.$[i].that.$[x].amnt": 1000}
var options = {arrayFilters: [{"i._id": weekId}, {"x.userId": r.userId}]}
myModel.update(conditions, update, options, function(err, rowsAffected){
// handler
}
结果:{ ok: 0, n: 0, nModified: 0 }
_id 和 userId 是模型架构中的 schema.Types.ObjectId。
我尝试使用 3.6.1 的最新文件更新 node_modules/mongodb。看来猫鼬使用3.0.1。打印到控制台 typeof(weekId) 或 typeof(r.userId) 显示字符串类型。我相信这会根据架构进行更改,但无论哪种方式,我都尝试在这些项目上调用 mongoose.Types.ObjectId(weekId) 以查看是否解决了它,但没有运气。
MongoDB:3.6.1 猫鼬:5.0.0-rc2
【问题讨论】:
标签: javascript node.js mongodb express mongoose