我如何从猫鼬主模型中获取嵌套的架构键?

时间:2019-04-11 13:03:37

标签: node.js mongodb mongoose

我需要获取构成特定模型的嵌套属性/键。 例如模式:

const mongoose = require('mongoose');

const subDoc = mongoose.Schema({
    name: String,
    address: {
        street: String,
        no: Number
    }
});

const mainDoc = mongoose.Schema({
    subField: [subDoc],
    phone: Number,
});

console.log(mainDoc.paths)console.log(mainDoc.tree)仅打印“ subField”和“ phone”键。如果可能,从mainDoc获取subDoc键。 像这样:

subField
  name
  address
    street
    no
phone

1 个答案:

答案 0 :(得分:1)

我安慰mainDoc,这个mainDoc.childSchemas[0].schema.obj会给你

{ name: [Function: String],
  address: { street: [Function: String], no: [Function: Number] } }