As it stands, I am able to get this functionality working using the Mongo shell, but having issues with the nested arrays inside of Node.js using Mongoose.
Mongo shell command that is successful:
db.test.update({}, {$set: {"this.$[i].that.$[x].amnt": 10000}}, {arrayFilters: [{"i._id": ObjectId('5a568059bc44142a9ca33d09')}, {"x.userId": ObjectId('5a466acb864c752f8c9890c6')}]})
Result: changed the amnt field to 10000. There was no matches returned until calling ObjectId('') on the given strings pulled from the database.
Node.js code:
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
}
Result: { ok: 0, n: 0, nModified: 0 }
_id and userId are schema.Types.ObjectId in the schema for the model.
I tried updating the node_modules/mongodb with the latest files for 3.6.1. It appears mongoose uses 3.0.1. Printing out to console typeof(weekId) or typeof(r.userId) shows type of string. I believe this gets changed based on the schema, but either way I tried calling mongoose.Types.ObjectId(weekId) on these items to see if this resolved it, but no luck.
MongoDB: 3.6.1
Mongoose: 5.0.0-rc2
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…