Wilbeibi Wilbeibi - 1 year ago 132
Node.js Question

Mongoose: Generate Schema from existing data

For example, I have

collection in db:

{'_id':0, 'name': 'Joe', 'score':[80, 33]}
{'_id':1, 'name': 'Moe', 'score':[90, 81]}
... ...

How can I read this data with existing format, which means, use it's existing schema without create a new one.

I read Mongoose doc and googled for a while but didn't find satisfied answer.

Answer Source

It will work if you make a model with the same schema.

var schema = new mongoose.Schema({ name: 'string', score: [] });
var user = mongoose.model('User', schema);


Mongoose is an ODM, so you need a schema to create objects. If you need to run queries and get raw data from the database, I would stick with this library:


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download