假设我有以下架构
var userSchema = new Schema({
name : String
});
var User = mongoose.model('User',userSchema);
编辑:如果用户试图更新不存在的字段,我需要抛出异常。我的问题是如何检查更新文档中不存在更新字段。这是我需要的一个小例子:
app.post('/user/update/:id', function (req, res) {
var field = req.param('field'),
value = req.param('value'),
id = req.param('id');
User.findOne({_id: id},function(err, user){
if(err) throw err;
if (user) {
user[field] = value; // Here is I need to check that field is exists
// in user schema. If does't I have to throw
// an execption.
user.save(function (err){
return res.send(200);
});
}
})
});