node.js - Mongoose: Updating some/all fields in a Mongodb collection -
a settings form example.
on submit, appropriate collection needs updated; , 1, some, none or of fields need updated.
assuming i'm not mistaken, mongoose:
- ignores fields aren't defined in schema.
- does low-level validation on field based on schema (i.e. reject, (ignore?) value isn't of correct type).
so mean following advisable?
.put(function(req, res, next) { if (mongoose.types.objectid.isvalid(req.params._id)) { collection.update({_id: req.params._id}, { $set: req.body}, function (err, collection) { if (err) return next(err); res.send(200, {success: true}); }) }else{ res.send(400, {success: false}); } })
i.e. passing req.body
directly update?
it works, can't feel it's little lacking in validation/filtering? mongoose doing enough in schema approach sufficient? or should iterating on each expected field?
i'm happy set tests thought i'd check community , sanity check approach - suggested alternatives gratefully received.
as happens terrible practice collection.update
bypasses mongoose validation, 2 assumptions making false in case.
Comments
Post a Comment