getting schema attributes from Mongoose Model

后端 未结 9 1827
时光说笑
时光说笑 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:22

    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.

提交回复
热议问题