MongoDB /猫鼬|扩展架构功能

时间:2020-03-03 15:43:07

标签: javascript mongodb mongoose

我正在尝试为Schema提供一些自定义字段,以描述其行为。我想将其用于最小限度的“身份验证”,以获取某人需要读取其值的内容。 计划是像声明字段一样声明它,因此我将在字段声明中写“ minAuth:someLevel”。

我已经尝试过了,并且没有出错,但是我真的找不到任何地方的值。

有人知道我可以在哪里访问Schemas字段属性吗?

1 个答案:

答案 0 :(得分:0)

因此,如果您的架构看起来像这样:

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

然后您可以导入它,并使用模式变量

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

我希望这会有所帮助