Mongoose Document Model.prototype.base函数

Mongoose Document Model.prototype.base函数

Model.prototype.base 属性是Mongoose API中用于返回模型使用的基本Mongoose实例。

语法:

Model_Name.base

返回值: Model.prototype.base 属性返回模型使用的基本 Mongoose 实例。

设置Node.js应用程序:

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

npm init

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

npm install mongoose

项目结构: 项目的结构将如下所示:

Mongoose Document Model.prototype.base函数

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

  • 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.base 
console.log("Base is:",output)

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

node app.js

输出:

Base is: <ref *1> Mongoose {
  connections: [
    NativeConnection {
      base: [Circular *1],
      collections: [Object],
      models: [Object],
      config: {},
      replica: false,
      options: null,
      otherDbs: [],
      relatedDbs: {},
      states: [Object: null prototype],
      _readyState: 2,
      _closeCalled: false,
      _hasOpened: false,
      plugins: [],
      id: 0,
      _queue: [],
      _listening: false,
      _connectionString: 'mongodb://localhost:27017/geeksforgeeks',
      _connectionOptions: [Object],
      client: [MongoClient],
      '$initialConnection': [Promise]
    }
  ],
  models: { User: Model { User } },
  events: EventEmitter {
    _events: [Object: null prototype] {},
    _eventsCount: 0,
    _maxListeners: undefined,
    [Symbol(kCapture)]: false
  },
  __driver: {
    Binary: [Function: Binary] {
      fromExtendedJSON: [Function (anonymous)],
      BSON_BINARY_SUBTYPE_DEFAULT: 0,
      BUFFER_SIZE: 256,
      SUBTYPE_DEFAULT: 0,
      SUBTYPE_FUNCTION: 1,
      SUBTYPE_BYTE_ARRAY: 2,
      SUBTYPE_UUID_OLD: 3,
      SUBTYPE_UUID: 4,
      SUBTYPE_MD5: 5,
      SUBTYPE_ENCRYPTED: 6,
      SUBTYPE_COLUMN: 7,
      SUBTYPE_USER_DEFINED: 128
    },
    Collection: [Function: NativeCollection],
    Decimal128: [Function: Decimal128] {
      fromString: [Function (anonymous)],
      fromExtendedJSON: [Function (anonymous)]
    },
    ObjectId: [Function: ObjectId] {
      getInc: [Function (anonymous)],
      generate: [Function (anonymous)],
      createPk: [Function (anonymous)],
      createFromTime: [Function (anonymous)],
      createFromHexString: [Function (anonymous)],
      isValid: [Function (anonymous)],
      fromExtendedJSON: [Function (anonymous)],
      index: 15789560
    },
    ReadPreference: [Function: readPref],
    getConnection: [Function (anonymous)]
  },
  options: {
    pluralization: true,
    autoIndex: true,
    autoCreate: true,
    [Symbol(mongoose:default)]: true
  },
  _pluralize: [Function: pluralize],
  Schema: [Function: Schema] {
    reserved: [Object: null prototype] {
      validate: 1,
      toObject: 1,
      save: 1,
      remove: 1,
      populated: 1,
      isNew: 1,
      isModified: 1,
      init: 1,
      get: 1,
      errors: 1,
      collection: 1,
      removeListener: 1,
      listeners: 1,
      emit: 1,
      prototype: 1
    },
    Types: {
      String: [Function],
      Number: [Function],
      Boolean: [Function],
      DocumentArray: [Function],
      Subdocument: [Function],
      Array: [Function],
      Buffer: [Function],
      Date: [Function],
      ObjectId: [Function],
      Mixed: [Function],
      Decimal: [Function],
      Decimal128: [Function],
      Map: [Function],
      Oid: [Function],
      Object: [Function],
      Bool: [Function],
      ObjectID: [Function]
    },
    ObjectId: [Function: ObjectId] {
      schemaName: 'ObjectId',
      defaultOptions: {},
      get: [Function (anonymous)],
      set: [Function: set],
      _checkRequired: [Function (anonymous)],
      _cast: [Function: castObjectId],
      cast: [Function: cast],
      _defaultCaster: [Function (anonymous)],
      checkRequired: [Function (anonymous)]
    }
  },
  model: [Function (anonymous)],
  plugins: [
    [ [Function (anonymous)], [Object] ],
    [ [Function (anonymous)], [Object] ],
    [ [Function], [Object] ],
    [ [Function (anonymous)], [Object] ],
    [ [Function: trackTransaction], [Object] ]
  ],
  default: [Circular *1],
mongoose: [Circular *1]

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

  • 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 base property on Student model 
const output = Student.base  
console.log("Base is:", output)

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

node app.js

输出:

Base is: <ref *1> Mongoose {
  connections: [
    NativeConnection {
      base: [Circular *1],
      collections: [Object],
      models: [Object],
      config: {},
      replica: false,
      options: null,
      otherDbs: [],
      relatedDbs: {},
      states: [Object: null prototype],
      _readyState: 2,
      _closeCalled: false,
      _hasOpened: false,
      plugins: [],
      id: 0,
      _queue: [],
      _listening: false,
      _connectionString: 'mongodb://localhost:27017/geeksforgeeks',
      _connectionOptions: [Object],
      client: [MongoClient],
      '$initialConnection': [Promise]
    }
  ],
  models: { Student: Model { Student } },
  events: EventEmitter {
    _events: [Object: null prototype] {},
    _eventsCount: 0,
    _maxListeners: undefined,
    [Symbol(kCapture)]: false
  },
  __driver: {
    Binary: [Function: Binary] {
      fromExtendedJSON: [Function (anonymous)],
      BSON_BINARY_SUBTYPE_DEFAULT: 0,
      BUFFER_SIZE: 256,
      SUBTYPE_DEFAULT: 0,
      SUBTYPE_FUNCTION: 1,
      SUBTYPE_BYTE_ARRAY: 2,
      SUBTYPE_UUID_OLD: 3,
      SUBTYPE_UUID: 4,
      SUBTYPE_MD5: 5,
      SUBTYPE_ENCRYPTED: 6,
      SUBTYPE_COLUMN: 7,
      SUBTYPE_USER_DEFINED: 128
    },
    Collection: [Function: NativeCollection],
    Decimal128: [Function: Decimal128] {
      fromString: [Function (anonymous)],
      fromExtendedJSON: [Function (anonymous)]
    },
    ObjectId: [Function: ObjectId] {
      getInc: [Function (anonymous)],
      generate: [Function (anonymous)],
      createPk: [Function (anonymous)],
      createFromTime: [Function (anonymous)],
      createFromHexString: [Function (anonymous)],
      isValid: [Function (anonymous)],
      fromExtendedJSON: [Function (anonymous)],
      index: 8101215
    },
    ReadPreference: [Function: readPref],
    getConnection: [Function (anonymous)]
  },
  options: {
    pluralization: true,
    autoIndex: true,
    autoCreate: true,
    [Symbol(mongoose:default)]: true
  },
  _pluralize: [Function: pluralize],
  Schema: [Function: Schema] {
    reserved: [Object: null prototype] {
      validate: 1,
      toObject: 1,
      save: 1,
      remove: 1,
      populated: 1,
      isNew: 1,
      isModified: 1,
      init: 1,
      get: 1,
      errors: 1,
      collection: 1,
      removeListener: 1,
      listeners: 1,
      emit: 1,
      prototype: 1
    },
    Types: {
      String: [Function],
      Number: [Function],
      Boolean: [Function],
      DocumentArray: [Function],
      Subdocument: [Function],
      Array: [Function],
      Buffer: [Function],
      Date: [Function],
      ObjectId: [Function],
      Mixed: [Function],
      Decimal: [Function],
      Decimal128: [Function],
      Map: [Function],
      Oid: [Function],
      Object: [Function],
      Bool: [Function],
      ObjectID: [Function]
    },
    ObjectId: [Function: ObjectId] {
      schemaName: 'ObjectId',
      defaultOptions: {},
      get: [Function (anonymous)],
      set: [Function: set],
      _checkRequired: [Function (anonymous)],
      _cast: [Function: castObjectId],
      cast: [Function: cast],
      _defaultCaster: [Function (anonymous)],
      checkRequired: [Function (anonymous)]
    }
  },
  model: [Function (anonymous)],
  plugins: [
    [ [Function (anonymous)], [Object] ],
    [ [Function (anonymous)], [Object] ],
    [ [Function], [Object] ],
    [ [Function (anonymous)], [Object] ],
    [ [Function: trackTransaction], [Object] ]
  ],
  default: [Circular *1],
mongoose: [Circular *1]

参考: https://mongoosejs.com/docs/api/model.html#model_Model-base

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程