我使用expressjs,MongoDB的和猫鼬,我试图更新以下模式的计数对象:猫鼬模式,设置关键变量
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 }
}
})
,并且更新部分(不工作):
var key = 'trips' // set dynamically, could be 'videos' or 'followers'
Users.update({'username': username}, {$set: {'counts.key': 12}}, callback)
工作的例子,但不是动态:
Users.update({'username': username}, {$set: {'counts.trips': 12}}, callback)
任何想法?
可能重复[如何使用可变猫鼬查询?](http://stackoverflow.com/questions/21592595/how-to-use-variable-mongoose-query) – JohnnyHK
是的,谢谢!这是我正在寻找的。我想知道是否有其他方法可以做到这一点。 – inwpitrust