简体   繁体   English

MongoDB/猫鼬 | 扩展架构功能

[英]MongoDB/mongoose | Extending Schema functionality

I'am trying to give a Schema some custom fields, that describes its behaviour.我正在尝试为 Schema 提供一些自定义字段,以描述其行为。 I want to use it for some authentication, by giving a minumum "level", what someone needs to read its value.我想将它用于某些身份验证,通过提供最低“级别”,某人需要读取其值。 The plan is to declare it just like making a field required, so I would write "minAuth: someLevel" in the field declaration.计划是声明它就像制作一个必需的字段一样,所以我会在字段声明中写上“minAuth:someLevel”。

I already tried this and it gives no error, but I cant really find the value anywhere.我已经尝试过这个并且它没有给出任何错误,但我无法在任何地方找到它的价值。

Does somebody know, where I can access a Schemas fields properties?有人知道,我可以在哪里访问 Schemas 字段属性吗?

So if your schema looks something like this:因此,如果您的架构如下所示:

YourSchema = new mongoose.Schema({
  name: {type: 'String', minLevel: 1}
})
module.exports = mongoose.model('You', YourSchema);

Then you can import it, and use the schema variables like然后您可以导入它,并使用架构变量,如

const You = require("/path/to/schema/you")
// Get all fields from the schema
let path = You.schema.paths
  for ( const [index, PropertyName] of path) {
    const PropertyDescription = schema.paths[index]
    let Level = PropertyDescription.options.minLevel
  }


// Or you can go directly to it if you know the index of it

You.schema.paths[TheIndexYouKnow].options.NameOfYourCustomVariable

I hope this helps我希望这有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM