Laakal
Laakal

Reputation: 687

How do i get nested schema keys from mongoose main model?

I need to get the nested attributes/keys that make up a particular model. For example schema:

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) or console.log(mainDoc.tree) only print "subField" and "phone" keys. If its possible get subDoc keys form mainDoc. Like that:

subField
  name
  address
    street
    no
phone

Upvotes: 1

Views: 124

Answers (1)

Moad Ennagi
Moad Ennagi

Reputation: 1073

I consoled into mainDoc, this mainDoc.childSchemas[0].schema.obj would give you

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

Upvotes: 1

Related Questions