从猫鼬模型获取模式属性 [英] getting schema attributes from Mongoose Model

查看:30
本文介绍了从猫鼬模型获取模式属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Mongoose.js 创建带有模式的模型.

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?

例如

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

);

是否可以提取/识别模式的属性[SID, hats, team, shields, shoes]??

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

推荐答案

是的,这是可能的.

每个模式都有一个 paths 属性,看起来有点像这样(这是我的代码示例):

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

您也可以通过模型访问它.它在 Model.schema.paths 下.

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

这篇关于从猫鼬模型获取模式属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆