Open In App

Mongoose Document Model.prototype.schema API

Improve
Improve
Like Article
Like
Save
Share
Report

The Model.prototype.schema property of the Mongoose API is used to return the schema which the model uses.

Syntax:

Model_Name.schema

Returns: The Model.prototype.modelName property returns the schema of the model.

Setting up Node.js application:

Step 1: Create a Node.js application using the following command:

npm init

Step 2: After creating the NodeJS application, Install the required module using the following command:

npm install mongoose

Project Structure: The project structure will look like this:

 

Example 1: In this example, we have established a database connection using mongoose and defined model over userSchema, having two columns or fields “name” and “age”. In the end, we are using schema property on the User model which will return the schema used by the model.

  • app.js: Write down the below code in the app.js file:

Javascript




// Require mongoose module
const mongoose = require('mongoose');
  
// Set Up the Database connection
mongoose.connect(
    useNewUrlParser: true,
    useUnifiedTopology: true
})
  
const userSchema = new mongoose.Schema({ 
    name: String, 
    age: Number
})
  
// Defining userSchema model
const User = mongoose.model('User', userSchema);
  
const output = User.schema
console.log("Schema Name is:",output)


Steps to run the program: To run the application execute the below command from the root directory of the project:

node app.js

Output:

Schema is: Schema {
  obj: { name: [Function: String], age: [Function: Number] },
  paths: {
    name: SchemaString {
      enumValues: [],
      regExp: null,
      path: 'name',
      instance: 'String',
      validators: [],
      getters: [],
      setters: [],
      _presplitPath: [Array],
      options: [SchemaStringOptions],
      _index: null,
      [Symbol(mongoose#schemaType)]: true
    },
    age: SchemaNumber {
      path: 'age',
      instance: 'Number',
      validators: [],
      getters: [],
      setters: [],
      _presplitPath: [Array],
      options: [SchemaNumberOptions],
      _index: null,
      [Symbol(mongoose#schemaType)]: true
    },
    _id: ObjectId {
      path: '_id',
      instance: 'ObjectID',
      validators: [],
      getters: [],
      setters: [Array],
      _presplitPath: [Array],
      options: [SchemaObjectIdOptions],
      _index: null,
      defaultValue: [Function],
      [Symbol(mongoose#schemaType)]: true
    },
    __v: SchemaNumber {
      path: '__v',
      instance: 'Number',
      validators: [],
      getters: [],
      setters: [],
      _presplitPath: [Array],
      options: [SchemaNumberOptions],
      _index: null,
      [Symbol(mongoose#schemaType)]: true
    }
  },
  aliases: {},
  subpaths: {},
  virtuals: {
    id: VirtualType {
      path: 'id',
      getters: [Array],
      setters: [],
      options: {}
    }
  },
  singleNestedPaths: {},
  nested: {},
  inherits: {},
  callQueue: [],
  _indexes: [],
  methods: {},
  methodOptions: {},
  statics: {},
  tree: {
    name: [Function: String],
    age: [Function: Number],
    _id: { auto: true, type: 'ObjectId' },
    __v: [Function: Number],
    id: VirtualType {
      path: 'id',
      getters: [Array],
      setters: [],
      options: {}
    }
  },
  query: {},
  childSchemas: [],
  plugins: [
    { fn: [Function (anonymous)], opts: [Object] },
    { fn: [Function (anonymous)], opts: [Object] },
    { fn: [Function], opts: [Object] },
    { fn: [Function (anonymous)], opts: [Object] },
    { fn: [Function: trackTransaction], opts: [Object] }
  ],
  '$id': 1,
  mapPaths: [],
  s: { hooks: Kareem { _pres: [Map], _posts: [Map] } },
  _userProvidedOptions: {},
  options: {
    typeKey: 'type',
    id: true,
    _id: true,
    validateBeforeSave: true,
    read: null,
    shardKey: null,
    discriminatorKey: '__t',
    autoIndex: null,
    minimize: true,
    optimisticConcurrency: false,
    versionKey: '__v',
    capped: false,
    bufferCommands: true,
    strictQuery: true,
    strict: true,
    pluralization: true
  },
  '$globalPluginsApplied': true
}

Example 2: In this example, we have established a database connection using mongoose and defined model over studentSchema, having three columns or fields “name”, “father_name”, and “mother_name”. In the end, we are using schema property on the Student model which will return schema of the model.

  • app.js: Write down the below code in the app.js file:

Javascript




// Require mongoose module
const mongoose = require('mongoose');
  
// Set Up the Database connection
mongoose.connect(
    useNewUrlParser: true,
    useUnifiedTopology: true
})
  
const studentSchema = new mongoose.Schema({ 
    name: String, 
    father_name: String, 
    mother_name: String 
})
  
// Defining studentSchema model
const Student = mongoose.model('Student', studentSchema);
  
// Accessing schema property on Student model
const output = Student.schema
console.log("Schema is:", output)


Steps to run the program: To run the application execute the below command from the root directory of the project:

node app.js

Output:

Schema is: Schema {
 obj: {
   name: [Function: String],
   father_name: [Function: String],
   mother_name: [Function: String]
 },
 paths: {
   name: SchemaString {
     enumValues: [],
     regExp: null,
     path: 'name',
     instance: 'String',
     validators: [],
     getters: [],
     setters: [],
     _presplitPath: [Array],
     options: [SchemaStringOptions],
     _index: null,
     [Symbol(mongoose#schemaType)]: true
   },
   father_name: SchemaString {
     enumValues: [],
     regExp: null,
     path: 'father_name',
     instance: 'String',
     validators: [],
     getters: [],
     setters: [],
     _presplitPath: [Array],
     options: [SchemaStringOptions],
     _index: null,
     [Symbol(mongoose#schemaType)]: true
   },
   mother_name: SchemaString {
     enumValues: [],
     regExp: null,
     path: 'mother_name',
     instance: 'String',
     validators: [],
     getters: [],
     setters: [],
     _presplitPath: [Array],
     options: [SchemaStringOptions],
     _index: null,
     [Symbol(mongoose#schemaType)]: true
   },
   _id: ObjectId {
     path: '_id',
     instance: 'ObjectID',
     validators: [],
     getters: [],
     setters: [Array],
     _presplitPath: [Array],
     options: [SchemaObjectIdOptions],
     _index: null,
     defaultValue: [Function],
     [Symbol(mongoose#schemaType)]: true
   },
   __v: SchemaNumber {
     path: '__v',
     instance: 'Number',
     validators: [],
     getters: [],
     setters: [],
     _presplitPath: [Array],
     options: [SchemaNumberOptions],
     _index: null,
     [Symbol(mongoose#schemaType)]: true
   }
 },
 aliases: {},
 subpaths: {},
 virtuals: {
   id: VirtualType {
     path: 'id',
     getters: [Array],
     setters: [],
     options: {}
   }
 },
 singleNestedPaths: {},
 nested: {},
 inherits: {},
 callQueue: [],
 _indexes: [],
 methods: {},
 methodOptions: {},
 statics: {},
 tree: {
   name: [Function: String],
   father_name: [Function: String],
   mother_name: [Function: String],
   _id: { auto: true, type: 'ObjectId' },
   __v: [Function: Number],
   id: VirtualType {
     path: 'id',
     getters: [Array],
     setters: [],
     options: {}
   }
 },
 query: {},
 childSchemas: [],
 plugins: [
   { fn: [Function (anonymous)], opts: [Object] },
   { fn: [Function (anonymous)], opts: [Object] },
   { fn: [Function], opts: [Object] },
   { fn: [Function (anonymous)], opts: [Object] },
   { fn: [Function: trackTransaction], opts: [Object] }
 ],
 '$id': 1,
 mapPaths: [],
 s: { hooks: Kareem { _pres: [Map], _posts: [Map] } },
 _userProvidedOptions: {},
 options: {
   typeKey: 'type',
   id: true,
   _id: true,
   validateBeforeSave: true,
   read: null,
   shardKey: null,
   discriminatorKey: '__t',
   autoIndex: null,
   minimize: true,
   optimisticConcurrency: false,
   versionKey: '__v',
   capped: false,
   bufferCommands: true,
   strictQuery: true,
   strict: true,
   pluralization: true
 },
 '$globalPluginsApplied': true
}

Reference: https://mongoosejs.com/docs/api/model.html#model_Model-schema



Last Updated : 15 Sep, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads