获取Mongoose字段的数据类型

获取Mongoose字段的数据类型,mongoose,Mongoose,我正在尝试获取mongoose模式中字段的数据类型。以下是我的模型模式: const TestSchema = new mongoose.Schema({ name: { type: String, }, nested: [{ nr: { type: Number, }, }], }); 我想得到一个字符串值,表示每个字段的数据类型名称=='string'嵌套[0]。nr=='number'依此类推 我尝试过TestSchema.path.n

我正在尝试获取mongoose模式中字段的数据类型。以下是我的模型模式:

const TestSchema = new mongoose.Schema({
  name: {
    type: String,
  },
  nested: [{
    nr: {
      type: Number,
    },
  }],
});
我想得到一个字符串值,表示每个字段的数据类型<代码>名称=='string'
嵌套[0]。nr=='number'
依此类推


我尝试过
TestSchema.path.name.instance
,但它对
嵌套的
字段不起作用(而且它没有文档记录,所以可能不是一个好的解决方案)

您可以尝试使用返回SchemaType对象的
schema.path('path')

TestSchema.path('nested').schema.paths.nr.instance
此行将返回“Number”。
查看中的示例,您可以尝试使用返回SchemaType对象的
schema.path('path')

TestSchema.path('nested').schema.paths.nr.instance
此行将返回“Number”。
请查看

中的示例,谢谢,但这不适用于我的模式中的
嵌套
字段。我已编辑了我的答案。schema.path为mongoose返回一个具有多个属性的配置对象。嵌套对象位于“路径”属性内。谢谢,但这不适用于我的架构中的
嵌套
字段。我已编辑了我的答案。schema.path为mongoose返回一个具有多个属性的配置对象。嵌套对象位于“路径”属性内。