Mongoose不验证查询的模式

时间:2015-02-19 13:35:51

标签: node.js mongodb validation mongoose

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 })); });

如何对读取查询进行架构验证?

0 个答案:

没有答案