Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting schema attributes from Mongoose Model

I'm using Mongoose.js to create models with schemas.

I have a list of models (many) and at times I'd like to get the attributes/keys that make up a particular model.

Is there a method to pull out the attribute schemas for any given model?

For example,

var mySchema = module.exports = new Schema({   SID: {     type: Schema.Types.ObjectId     //, required: true   },   teams: {     type: [String]   },   hats: [{     val: String,     dt: Date   }],   shields: [{     val: String,     dt: Date   }],   shoes: [{     val: String,     dt: Date   }] } 

);

Is it possible to pull out/identify the attributes of the schema [SID, hats, teams, shields, shoes]??

like image 577
user1460015 Avatar asked Jun 11 '13 01:06

user1460015


People also ask

What is the difference between schema and model in Mongoose?

Mongoose Schema vs. Model. A Mongoose model is a wrapper on the Mongoose schema. A Mongoose schema defines the structure of the document, default values, validators, etc., whereas a Mongoose model provides an interface to the database for creating, querying, updating, deleting records, etc.

What is schema path Mongoose?

In Mongoose, a schema is a configuration object for a model. Schemas do not allow you to read and write from MongoDB, that's what models are for. But they do: Define what properties the documents you save in MongoDB can have. Define custom validation.

What does Mongoose model return?

mongoose. model() returns a Model ( It is a constructor, compiled from Schema definitions).

What does $Set do in Mongoose?

The $set operator replaces the value of a field with the specified value.


2 Answers

Yes, it is possible.

Each schema has a paths property, that looks somewhat like this (this is an example of my code):

paths: {     number: [Object],     'name.first': [Object],     'name.last': [Object],     ssn: [Object],     birthday: [Object],     'job.company': [Object],     'job.position': [Object],     'address.city': [Object],     'address.state': [Object],     'address.country': [Object],     'address.street': [Object],     'address.number': [Object],     'address.zip': [Object],     email: [Object],     phones: [Object],     tags: [Object],     createdBy: [Object],     createdAt: [Object],     updatedBy: [Object],     updatedAt: [Object],     meta: [Object],     _id: [Object],     __v: [Object] } 

You can access this through an model too. It's under Model.schema.paths.

like image 170
gustavohenke Avatar answered Oct 17 '22 07:10

gustavohenke


Don't have enough rep to comment, but this also spits out a list and loops through all of the schema types.

mySchema.schema.eachPath(function(path) {     console.log(path); }); 

should print out:

number name.first name.last ssn birthday job.company job.position address.city address.state address.country address.street address.number address.zip email phones tags createdBy createdAt updatedBy updatedAt meta _id __v 

Or you could get all Attributes as an Array like this:

var props = Object.keys(mySchema.schema.paths); 
like image 20
js_gandalf Avatar answered Oct 17 '22 06:10

js_gandalf