Mongoose似乎只在写入数据库时强制进行模式验证,而不是在查询时。更具体地说,查询仍会返回Schema
中未定义的属性。
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 })); });
如何对读取查询进行架构验证?