mongoose:从现有的数据生成模式
例如,我有user
集合在分贝:
{'_id':0, 'name': 'Joe', 'score':[80, 33]} {'_id':1, 'name': 'Moe', 'score':[90, 81]} ... ...
我怎样才能用现有的格式来读取这些数据,也就是说,使用现有的模式而不需要创build一个新的模式。
我读了一下Mongoose doc并search了一下,但是没有find满意的答案。
如果您使用相同的模式创build模型,它将工作。
var schema = new mongoose.Schema({ name: 'string', score: [] }); var user = mongoose.model('User', schema);
编辑:
Mongoose是一个ODM,所以你需要一个模式来创build对象。 如果你需要运行查询并从数据库中获取原始数据,我会坚持使用这个库:
将mongoose与现有集合一起使用时,必须在mongoose模式中引用该集合。 这样做的方法是将集合的名称添加到您的架构。 所以如果你的集合是在'mongodb:// localhost:27017 / test'中,并且调用了'things',你可以使用:
~~~ const Schema = mongoose.Schema; const ThingSchema = new Schema({ name: { type: String } }); const Model = mongoose.model('Thing', ThingSchema, 'things'); module.exports = Model; ~~~
感谢http://chrisflx.blogspot.fr/2014/04/nodejs-mongoose-with-preexisting-data.html?m=1