Mongoose Document Model.prototype.schema函数

Mongoose Document Model.prototype.schema函数

Mongoose API的 Model.prototype.schema 属性用于返回模型使用的架构。

语法:

Model_Name.schema

返回值: Model.prototype.modelName 属性返回模型的模式。

设置Node.js应用:

步骤1: 使用以下命令创建一个Node.js应用:

npm init

步骤2: 创建 NodeJS 应用程序后,使用以下命令安装所需模块:

npm install mongoose

项目结构: 项目的结构将看起来像这样:

Mongoose Document Model.prototype.schema函数

示例1: 在此示例中,我们使用mongoose建立了一个数据库连接,并在userSchema上定义了一个模型,该模型有两个列或字段“name”和“age”。最后,我们使用User模型上的schema属性,该属性将返回模型使用的架构。

  • app.js: 将以下代码写入app.js文件中:
// Require mongoose module 
const mongoose = require('mongoose'); 
  
// Set Up the Database connection 
mongoose.connect( 
    'mongodb://localhost:27017/geeksforgeeks', { 
    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)

运行程序的步骤: 从项目的根目录中执行以下命令来运行应用程序:

node app.js

输出:

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
}

示例2: 在这个示例中,我们使用mongoose建立了一个数据库连接,并在studentSchema上定义了一个模型,其中有三个列或字段“name”,“father_name”和“mother_name”。最后,我们在Student模型上使用schema属性,它将返回模型的模式。

  • app.js: 在app.js文件中写下以下代码:
// Require mongoose module 
const mongoose = require('mongoose'); 
  
// Set Up the Database connection 
mongoose.connect( 
    'mongodb://localhost:27017/geeksforgeeks', { 
    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)

运行程序的步骤:

要运行应用程序,请从项目的根目录执行以下命令:

node app.js

输出:

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
}

参考链接: https://mongoosejs.com/docs/api/model.html#model_Model-schema

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程