getting schema attributes from Mongoose Model

后端 未结 9 1834
时光说笑
时光说笑 2020-12-01 06:19

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 mo

9条回答
  •  天涯浪人
    2020-12-01 06:30

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

提交回复
热议问题