Mongoose seems to force schema validation only when writing on the database, not when querying it. More specifically, attributes not defined in the Schema
are still returned by queries.
var mongoose = require("mongoose");
// MongoDB collection "test.objs" is filled with one object: { a: 1 }
mongoose.connect("mongodb://localhost:27017/test");
schema = new mongoose.Schema({ b: Number });
model = mongoose.model("obj",schema);
// Prints {}, as expected
console.log(new model({ a: 1 }));
// Prints { a: 1 } but I expect {}
model.find(function(e,val){ console.log(val[0]) });
model.find(function(e,val){ console.log(val[0].toObject({ strict: true })); });
How to do schema validation on read queries?
Aucun commentaire:
Enregistrer un commentaire