Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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]??

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.1k views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...