I am currently utilizing expressjs, mongodb, and mongoose. My goal is to update the counts object within the given schema:
var UsersSchema = new Schema({
username: { type: String, required: true },
counts: {
followers: { type: Number, default: 0 },
trips: { type: Number, default: 0 },
videos: { type: Number, default: 0 }
}
})
The issue lies in the update part of the code (not functioning as expected):
var key = 'trips' // set dynamically, could be 'videos' or 'followers'
Users.update({'username': username}, {$set: {'counts.key': 12}}, callback)
A working example that is not dynamic:
Users.update({'username': username}, {$set: {'counts.trips': 12}}, callback)
Any suggestions on how to solve this?