2023-04-04

打印mongoose

const mongoose = require('mongoose') 引入mongoose模块会自动创建mongoose实例对象。

  • mongoose是一个对象
  • Mongoose.connections 属性
  • mongoose.Schema :构造函数
  • Mongoose.model: 函数
Mongoose {
  connections: [
    NativeConnection {
      base: [Circular *1],
      collections: {},
      models: {},
      config: {},
      replica: false,
      options: null,
      otherDbs: [],
      relatedDbs: {},
      states: [Object: null prototype],
      _readyState: 0,
      _closeCalled: false,
      _hasOpened: false,
      plugins: [],
      id: 0,
      _queue: [],
      _listening: false
    }
  ],
  nextConnectionId: 1,
  models: {},
  events: EventEmitter {
    _events: [Object: null prototype] {},
    _eventsCount: 0,
    _maxListeners: undefined,
    [Symbol(kCapture)]: false
  },
  __driver: {
    Collection: [Function: NativeCollection],
    Connection: [Function: NativeConnection] { STATES: [Object: null prototype] }
  },
  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],
      UUID: [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: removeSubdocs], [Object] ],
    [ [Function: saveSubdocs], [Object] ],
    [ [Function], [Object] ],
    [ [Function: trackTransaction], [Object] ],
    [ [Function: validateBeforeSave], [Object] ]
  ],
  default: [Circular *1],
  mongoose: [Circular *1],
  cast: [Function: cast],
  STATES: [Object: null prototype] {
    '0': 'disconnected',
    '1': 'connected',
    '2': 'connecting',
    '3': 'disconnecting',
    '99': 'uninitialized',
    disconnected: 0,
    connected: 1,
    connecting: 2,
    disconnecting: 3,
    uninitialized: 99
  },
  setDriver: [Function: setDriver],
  set: [Function (anonymous)],
  get: [Function (anonymous)],
  createConnection: [Function (anonymous)],
  connect: [AsyncFunction: connect],
  disconnect: [AsyncFunction: disconnect],
  startSession: [Function (anonymous)],
  pluralize: [Function (anonymous)],
  deleteModel: [Function (anonymous)],
  modelNames: [Function (anonymous)],
  plugin: [Function (anonymous)],
  version: '7.0.3',
  Mongoose: [Function: Mongoose],
  SchemaType: [Function: SchemaType] {
    cast: [Function: cast],
    set: [Function: set],
    get: [Function (anonymous)],
    _isRef: [Function (anonymous)],
    checkRequired: [Function (anonymous)],
    CastError: [class CastError extends MongooseError],
    ValidatorError: [class ValidatorError extends MongooseError]
  },
  SchemaTypes: {
    String: [Function: SchemaString] {
      schemaName: 'String',
      defaultOptions: {},
      _cast: [Function: castString],
      cast: [Function: cast],
      _defaultCaster: [Function (anonymous)],
      get: [Function (anonymous)],
      set: [Function: set],
      _checkRequired: [Function (anonymous)],
      checkRequired: [Function (anonymous)]
    },
    Number: [Function: SchemaNumber] {
      get: [Function (anonymous)],
      set: [Function: set],
      _cast: [Function: castNumber],
      cast: [Function: cast],
      _defaultCaster: [Function (anonymous)],
      schemaName: 'Number',
      defaultOptions: {},
      _checkRequired: [Function (anonymous)],
      checkRequired: [Function (anonymous)]
    },
    Boolean: [Function: SchemaBoolean] {
      schemaName: 'Boolean',
      defaultOptions: {},
      _cast: [Function],
      set: [Function: set],
      cast: [Function: cast],
      _defaultCaster: [Function (anonymous)],
      _checkRequired: [Function (anonymous)],
      checkRequired: [Function (anonymous)],
      '$conditionalHandlers': [Object]
    },
    DocumentArray: [Function: DocumentArrayPath] {
      schemaName: 'DocumentArray',
      options: [Object],
      defaultOptions: {},
      set: [Function: set]
    },
    Subdocument: [Function: SubdocumentPath] {
      defaultOptions: {},
      set: [Function: set]
    },
    Array: [Function: SchemaArray] {
      schemaName: 'Array',
      options: [Object],
      defaultOptions: {},
      set: [Function: set],
      _checkRequired: [Function (anonymous)],
      checkRequired: [Function (anonymous)]
    },
    Buffer: [Function: SchemaBuffer] {
      schemaName: 'Buffer',
      defaultOptions: {},
      _checkRequired: [Function (anonymous)],
      set: [Function: set],
      checkRequired: [Function (anonymous)]
    },
    Date: [Function: SchemaDate] {
      schemaName: 'Date',
      defaultOptions: {},
      _cast: [Function: castDate],
      set: [Function: set],
      cast: [Function: cast],
      _defaultCaster: [Function (anonymous)],
      _checkRequired: [Function (anonymous)],
      checkRequired: [Function (anonymous)]
    },
    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)]
    },
    Mixed: [Function: Mixed] {
      schemaName: 'Mixed',
      defaultOptions: {},
      get: [Function (anonymous)],
      set: [Function: set]
    },
    Decimal: [Function: Decimal128] {
      schemaName: 'Decimal128',
      defaultOptions: {},
      _cast: [Function: castDecimal128],
      set: [Function: set],
      cast: [Function: cast],
      _defaultCaster: [Function (anonymous)],
      _checkRequired: [Function (anonymous)],
      checkRequired: [Function (anonymous)]
    },
    Decimal128: [Function: Decimal128] {
      schemaName: 'Decimal128',
      defaultOptions: {},
      _cast: [Function: castDecimal128],
      set: [Function: set],
      cast: [Function: cast],
      _defaultCaster: [Function (anonymous)],
      _checkRequired: [Function (anonymous)],
      checkRequired: [Function (anonymous)]
    },
    Map: [class Map extends SchemaType] {
      schemaName: 'Map',
      defaultOptions: {}
    },
    UUID: [Function: SchemaUUID] {
      schemaName: 'UUID',
      defaultOptions: {},
      _cast: [Function (anonymous)],
      set: [Function: set],
      cast: [Function: cast],
      _checkRequired: [Function (anonymous)],
      checkRequired: [Function (anonymous)]
    },
    Oid: [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)]
    },
    Object: [Function: Mixed] {
      schemaName: 'Mixed',
      defaultOptions: {},
      get: [Function (anonymous)],
      set: [Function: set]
    },
    Bool: [Function: SchemaBoolean] {
      schemaName: 'Boolean',
      defaultOptions: {},
      _cast: [Function],
      set: [Function: set],
      cast: [Function: cast],
      _defaultCaster: [Function (anonymous)],
      _checkRequired: [Function (anonymous)],
      checkRequired: [Function (anonymous)],
      '$conditionalHandlers': [Object]
    },
    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)]
    }
  },
  VirtualType: [Function: VirtualType],
  Types: {
    Array: [Function: MongooseArray],
    Buffer: [Function: MongooseBuffer] {
      pathSymbol: Symbol(mongoose#Buffer#_path),
      mixin: [Object],
      Binary: [Function]
    },
    Embedded: [Function: ArraySubdocument] {
      _events: undefined,
      _eventsCount: 0,
      _maxListeners: undefined,
      setMaxListeners: [Function: setMaxListeners],
      getMaxListeners: [Function: getMaxListeners],
      emit: [Function: emit],
      addListener: [Function: addListener],
      on: [Function: addListener],
      prependListener: [Function: prependListener],
      once: [Function: once],
      prependOnceListener: [Function: prependOnceListener],
      removeListener: [Function: removeListener],
      off: [Function: removeListener],
      removeAllListeners: [Function: removeAllListeners],
      listeners: [Function: listeners],
      rawListeners: [Function: rawListeners],
      listenerCount: [Function: listenerCount],
      eventNames: [Function: eventNames]
    },
    Document: [Function: ArraySubdocument] {
      _events: undefined,
      _eventsCount: 0,
      _maxListeners: undefined,
      setMaxListeners: [Function: setMaxListeners],
      getMaxListeners: [Function: getMaxListeners],
      emit: [Function: emit],
      addListener: [Function: addListener],
      on: [Function: addListener],
      prependListener: [Function: prependListener],
      once: [Function: once],
      prependOnceListener: [Function: prependOnceListener],
      removeListener: [Function: removeListener],
      off: [Function: removeListener],
      removeAllListeners: [Function: removeAllListeners],
      listeners: [Function: listeners],
      rawListeners: [Function: rawListeners],
      listenerCount: [Function: listenerCount],
      eventNames: [Function: eventNames]
    },
    DocumentArray: [Function: MongooseDocumentArray],
    Decimal128: [class Decimal128 extends BSONValue],
    ObjectId: [class ObjectId extends BSONValue] { index: 7107676 },
    Map: [class MongooseMap extends Map],
    Subdocument: [Function: Subdocument]
  },
  Query: [Function: Query] {
    base: {
      toConstructor: [Function: toConstructor],
      setOptions: [Function (anonymous)],
      collection: [Function: collection],
      collation: [Function (anonymous)],
      '$where': [Function (anonymous)],
      where: [Function (anonymous)],
      equals: [Function: equals],
      eq: [Function: eq],
      or: [Function: or],
      nor: [Function: nor],
      and: [Function: and],
      gt: [Function (anonymous)],
      gte: [Function (anonymous)],
      lt: [Function (anonymous)],
      lte: [Function (anonymous)],
      ne: [Function (anonymous)],
      in: [Function (anonymous)],
      nin: [Function (anonymous)],
      all: [Function (anonymous)],
      regex: [Function (anonymous)],
      size: [Function (anonymous)],
      maxDistance: [Function (anonymous)],
      minDistance: [Function (anonymous)],
      mod: [Function (anonymous)],
      exists: [Function (anonymous)],
      elemMatch: [Function (anonymous)],
      within: [Function: within],
      box: [Function (anonymous)],
      polygon: [Function (anonymous)],
      circle: [Function (anonymous)],
      near: [Function: near],
      intersects: [Function: intersects],
      geometry: [Function: geometry],
      select: [Function: select],
      slice: [Function (anonymous)],
      sort: [Function (anonymous)],
      limit: [Function (anonymous)],
      skip: [Function (anonymous)],
      batchSize: [Function (anonymous)],
      comment: [Function (anonymous)],
      maxTimeMS: [Function (anonymous)],
      maxTime: [Function (anonymous)],
      hint: [Function (anonymous)],
      j: [Function: j],
      slaveOk: [Function (anonymous)],
      setReadPreference: [Function (anonymous)],
      read: [Function (anonymous)],
      r: [Function (anonymous)],
      readConcern: [Function (anonymous)],
      tailable: [Function (anonymous)],
      w: [Function: writeConcern],
      writeConcern: [Function: writeConcern],
      wTimeout: [Function: wtimeout],
      wtimeout: [Function: wtimeout],
      merge: [Function (anonymous)],
      find: [Function (anonymous)],
      _find: [AsyncFunction: _find],
      cursor: [Function (anonymous)],
      findOne: [Function (anonymous)],
      _findOne: [AsyncFunction: _findOne],
      count: [Function (anonymous)],
      _count: [AsyncFunction: _count],
      distinct: [Function (anonymous)],
      _distinct: [AsyncFunction: _distinct],
      updateMany: [Function: updateMany],
      _updateMany: [AsyncFunction (anonymous)],
      updateOne: [Function: updateOne],
      _updateOne: [AsyncFunction (anonymous)],
      replaceOne: [Function: replaceOne],
      _replaceOne: [AsyncFunction (anonymous)],
      deleteOne: [Function (anonymous)],
      _deleteOne: [AsyncFunction (anonymous)],
      deleteMany: [Function (anonymous)],
      _deleteMany: [AsyncFunction (anonymous)],
      findOneAndUpdate: [Function (anonymous)],
      _findOneAndUpdate: [AsyncFunction (anonymous)],
      findOneAndDelete: [Function (anonymous)],
      findOneAndRemove: [Function (anonymous)],
      _findOneAndRemove: [AsyncFunction (anonymous)],
      setTraceFunction: [Function (anonymous)],
      exec: [AsyncFunction: exec],
      then: [AsyncFunction (anonymous)],
      selected: [Function: selected],
      selectedInclusively: [Function: selectedInclusively],
      selectedExclusively: [Function: selectedExclusively],
      _mergeUpdate: [Function (anonymous)],
      _optionsForExec: [Function (anonymous)],
      _fieldsForExec: [Function (anonymous)],
      _updateForExec: [Function (anonymous)],
      _ensurePath: [Function (anonymous)],
      _validate: [Function (anonymous)]
    },
    'use$geoWithin': true
  },
  Model: Model { undefined },
  Document: [Function: Document] {
    _events: undefined,
    _eventsCount: 0,
    _maxListeners: undefined,
    setMaxListeners: [Function: setMaxListeners],
    getMaxListeners: [Function: getMaxListeners],
    emit: [Function: emit],
    addListener: [Function: addListener],
    on: [Function: addListener],
    prependListener: [Function: prependListener],
    once: [Function: once],
    prependOnceListener: [Function: prependOnceListener],
    removeListener: [Function: removeListener],
    off: [Function: removeListener],
    removeAllListeners: [Function: removeAllListeners],
    listeners: [Function: listeners],
    rawListeners: [Function: rawListeners],
    listenerCount: [Function: listenerCount],
    eventNames: [Function: eventNames],
    ValidationError: [class ValidationError extends MongooseError]
  },
  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)]
  },
  isValidObjectId: [Function (anonymous)],
  isObjectIdOrHexString: [Function (anonymous)],
  syncIndexes: [Function (anonymous)],
  Decimal128: [Function: Decimal128] {
    schemaName: 'Decimal128',
    defaultOptions: {},
    _cast: [Function: castDecimal128],
    set: [Function: set],
    cast: [Function: cast],
    _defaultCaster: [Function (anonymous)],
    _checkRequired: [Function (anonymous)],
    checkRequired: [Function (anonymous)]
  },
  Mixed: [Function: Mixed] {
    schemaName: 'Mixed',
    defaultOptions: {},
    get: [Function (anonymous)],
    set: [Function: set]
  },
  Date: [Function: SchemaDate] {
    schemaName: 'Date',
    defaultOptions: {},
    _cast: [Function: castDate],
    set: [Function: set],
    cast: [Function: cast],
    _defaultCaster: [Function (anonymous)],
    _checkRequired: [Function (anonymous)],
    checkRequired: [Function (anonymous)]
  },
  Number: [Function: SchemaNumber] {
    get: [Function (anonymous)],
    set: [Function: set],
    _cast: [Function: castNumber],
    cast: [Function: cast],
    _defaultCaster: [Function (anonymous)],
    schemaName: 'Number',
    defaultOptions: {},
    _checkRequired: [Function (anonymous)],
    checkRequired: [Function (anonymous)]
  },
  Error: [class MongooseError extends Error] {
    messages: {
      DocumentNotFoundError: null,
      general: [Object],
      Number: [Object],
      Date: [Object],
      String: [Object]
    },
    Messages: {
      DocumentNotFoundError: null,
      general: [Object],
      Number: [Object],
      Date: [Object],
      String: [Object]
    },
    DocumentNotFoundError: [class DocumentNotFoundError extends MongooseError],
    CastError: [class CastError extends MongooseError],
    ValidationError: [class ValidationError extends MongooseError],
    ValidatorError: [class ValidatorError extends MongooseError],
    VersionError: [class VersionError extends MongooseError],
    ParallelSaveError: [class ParallelSaveError extends MongooseError],
    OverwriteModelError: [class OverwriteModelError extends MongooseError],
    MissingSchemaError: [class MissingSchemaError extends MongooseError],
    MongooseServerSelectionError: [class MongooseServerSelectionError extends MongooseError],
    DivergentArrayError: [class DivergentArrayError extends MongooseError],
    StrictModeError: [class StrictModeError extends MongooseError],
    StrictPopulateError: [class StrictPopulateError extends MongooseError]
  },
  now: [Function: now],
  CastError: [class CastError extends MongooseError],
  SchemaTypeOptions: [class SchemaTypeOptions],
  mongo: {
    BSON: [Getter],
    Binary: [Getter],
    BSONRegExp: [Getter],
    BSONSymbol: [Getter],
    BSONType: [Getter],
    Code: [Getter],
    DBRef: [Getter],
    Decimal128: [Getter],
    Double: [Getter],
    Int32: [Getter],
    Long: [Getter],
    MaxKey: [Getter],
    MinKey: [Getter],
    ObjectId: [Getter],
    Timestamp: [Getter],
    MongoBulkWriteError: [Getter],
    ChangeStreamCursor: [Getter],
    MongoAPIError: [Getter],
    MongoAWSError: [Getter],
    MongoBatchReExecutionError: [Getter],
    MongoChangeStreamError: [Getter],
    MongoCompatibilityError: [Getter],
    MongoCursorExhaustedError: [Getter],
    MongoCursorInUseError: [Getter],
    MongoDecompressionError: [Getter],
    MongoDriverError: [Getter],
    MongoError: [Getter],
    MongoExpiredSessionError: [Getter],
    MongoGridFSChunkError: [Getter],
    MongoGridFSStreamError: [Getter],
    MongoInvalidArgumentError: [Getter],
    MongoKerberosError: [Getter],
    MongoMissingCredentialsError: [Getter],
    MongoMissingDependencyError: [Getter],
    MongoNetworkError: [Getter],
    MongoNetworkTimeoutError: [Getter],
    MongoNotConnectedError: [Getter],
    MongoParseError: [Getter],
    MongoRuntimeError: [Getter],
    MongoServerClosedError: [Getter],
    MongoServerError: [Getter],
    MongoServerSelectionError: [Getter],
    MongoSystemError: [Getter],
    MongoTailableCursorError: [Getter],
    MongoTopologyClosedError: [Getter],
    MongoTransactionError: [Getter],
    MongoUnexpectedServerResponseError: [Getter],
    MongoWriteConcernError: [Getter],
    AbstractCursor: [Getter],
    Admin: [Getter],
    AggregationCursor: [Getter],
    CancellationToken: [Getter],
    ChangeStream: [Getter],
    ClientSession: [Getter],
    Collection: [Getter],
    Db: [Getter],
    FindCursor: [Getter],
    GridFSBucket: [Getter],
    GridFSBucketReadStream: [Getter],
    GridFSBucketWriteStream: [Getter],
    ListCollectionsCursor: [Getter],
    ListIndexesCursor: [Getter],
    MongoClient: [Getter],
    OrderedBulkOperation: [Getter],
    UnorderedBulkOperation: [Getter],
    BatchType: [Getter],
    GSSAPICanonicalizationValue: [Getter],
    AuthMechanism: [Getter],
    Compressor: [Getter],
    CURSOR_FLAGS: [Getter],
    AutoEncryptionLoggerLevel: [Getter],
    MongoErrorLabel: [Getter],
    ExplainVerbosity: [Getter],
    ServerApiVersion: [Getter],
    ReturnDocument: [Getter],
    ProfilingLevel: [Getter],
    ReadConcernLevel: [Getter],
    ReadPreferenceMode: [Getter],
    ServerType: [Getter],
    TopologyType: [Getter],
    ReadConcern: [Getter],
    ReadPreference: [Getter],
    WriteConcern: [Getter],
    CommandFailedEvent: [Getter],
    CommandStartedEvent: [Getter],
    CommandSucceededEvent: [Getter],
    ConnectionCheckedInEvent: [Getter],
    ConnectionCheckedOutEvent: [Getter],
    ConnectionCheckOutFailedEvent: [Getter],
    ConnectionCheckOutStartedEvent: [Getter],
    ConnectionClosedEvent: [Getter],
    ConnectionCreatedEvent: [Getter],
    ConnectionPoolClearedEvent: [Getter],
    ConnectionPoolClosedEvent: [Getter],
    ConnectionPoolCreatedEvent: [Getter],
    ConnectionPoolMonitoringEvent: [Getter],
    ConnectionPoolReadyEvent: [Getter],
    ConnectionReadyEvent: [Getter],
    ServerClosedEvent: [Getter],
    ServerDescriptionChangedEvent: [Getter],
    ServerHeartbeatFailedEvent: [Getter],
    ServerHeartbeatStartedEvent: [Getter],
    ServerHeartbeatSucceededEvent: [Getter],
    ServerOpeningEvent: [Getter],
    TopologyClosedEvent: [Getter],
    TopologyDescriptionChangedEvent: [Getter],
    TopologyOpeningEvent: [Getter],
    SrvPollingEvent: [Getter]
  },
  mquery: [Function: Query] {
    permissions: {
      distinct: [Function],
      findOneAndRemove: [Function],
      findOneAndUpdate: [Function],
      count: [Function]
    },
    _isPermitted: [Function (anonymous)],
    canMerge: [Function (anonymous)],
    setGlobalTraceFunction: [Function (anonymous)],
    utils: {
      clone: [Function: clone],
      cloneObject: [Function: cloneObject],
      cloneArray: [Function: cloneArray],
      merge: [Function: merge],
      mergeClone: [Function: mergeClone],
      readPref: [Function: readPref],
      readConcern: [Function: readConcern],
      toString: [Function (anonymous)],
      isObject: [Function (anonymous)],
      keys: [Function: keys],
      create: [Function: create],
      inherits: [Function (anonymous)],
      isArgumentsObject: [Function (anonymous)]
    },
    env: { isNode: [Array], isMongo: false, isBrowser: false, type: 'node' },
    Collection: [class NodeCollection extends Collection],
    BaseCollection: [Function: Collection] { methods: [Array] }
  },
  sanitizeFilter: [Function: sanitizeFilter],
  trusted: [Function: trusted],
  skipMiddlewareFunction: [Function: skipWrappedFunction],
  overwriteMiddlewareResult: [Function: overwriteResult]
}

mongoose是什么

Mongoose 是 MongoDB 的对象建模工具。

Mongoose 用于和MongoDB 通信(增删改查)。

Mongoose运行在Node.js 平台。

  • Mongoose()是一个构造函数
  • 引入mongoose模块会自动创建一个mongoose实例对象。

mongoose.connect()

定义

mongoose.connect()用于连接MongoDB数据库。

它是一个异步函数。

语法

mongoose.connect(uri)
mongoose.connect(数据库连接字符串)

返回值

返回一个Promise对象

示例

promise.then()

定义

then()方法为promise绑定回调函数。

语法

promise.then(resolved,rejected)
promise.then((data) => {
  //在异步操作成功时,处理异步操作的结果
},(error) => {
  //在异步操作失败时,输出错误的原因
})

//一个参数:大多时候我们更加关注异步操作成功
promise.then((resolved) => {
  //在异步操作成功时,处理异步操作的结果
})

//then()方法可以链式调用
promise.then((resolved) => {
  //在异步操作成功时,处理异步操作的结果
})
  .then((resolved) => {})
    .then((resolved) => {})
    .catch(error => {//捕获错误信息})

then() 方法最多接受两个参数:

  • resolved :Promise 已完成的回调函数。

  • rejected: Promise已拒绝的回调函数。

返回值

  • 返回一个等效的prmose对象。

示例

promise.catch()

定义

catch()方法用于为promise对象绑定请求失败时的回调函数。

catch()方法用于捕获错误信息。

语法

promise.catch((err) => {})

返回值

返回一个promise对象。

示例

mongoose.model()

定义

用于创建与MongoDB数据库通信的工具。

该工具在这里意思就是:指一个构造函数。

构造函数可以实例化一个对象。

通过实例对象的方法与数据库进行通信。

语法

const Blog = mongoose.model('模型名称',模型原型)
const Blog = mongoose.model('模型名称',blogSchema)

返回值

返回大写的构造函数(Model())

示例

Schema()

定义

Schema()是一个构造函数/类。

Schema()用于描述数据表的结构。

  • 数据库:由多个数据库表构成
  • 数据库表:由多个document(记录)构成。
  • Document: 由字段和值构成。

语法

 new Schema({定义表结构},{选项对象})

const blogSchema = new Schema({
  //定义title字段
  title: {
    //字段类型:字符串
    type: String,
    //字段是否必写: true
    required: true
  },
  body: {
    type: String,
    required: true
  }
}, {})
  • 定义表结构: 定义有哪些字段、每个字段的数据类型、是否必填等
  • 选项对象:提供了一些参数,用来配置第一个参数

返回值

返回schema实例对象,表示数据结构。

示例

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 201,784评论 5 474
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,745评论 2 378
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 148,702评论 0 335
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,229评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,245评论 5 363
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,376评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,798评论 3 393
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,471评论 0 256
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,655评论 1 295
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,485评论 2 318
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,535评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,235评论 3 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,793评论 3 304
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,863评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,096评论 1 258
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,654评论 2 348
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,233评论 2 341

推荐阅读更多精彩内容

  • 首先,我们需要创建一个nestjs项目: 然后,我们需要安装sqlite3依赖库: 接下来,我们需要创建一个use...
    CoderAzreal阅读 171评论 0 0
  • 使用Node.js开发App的步骤 创建项目目录mkdir myappcd myapp# 合并以上两步骤mkdir...
    劳模阅读 78评论 0 0
  • 使用Node.js开发APP的步骤 1.创建项目目录 2.初始化 3.创建项目结构 4.安装模块 5.引入模块 6...
    贝兼全_c5e4阅读 92评论 0 0
  • 为什么要学习nodejs 原因 因为这是一门后台开发语言,用js来写得后台语言,使用ecma规范,前端人员学习起来...
    zyghhhh阅读 399评论 0 1
  • 1 命令行窗口 1.1. 命令行窗口(小黑屏)、CMD窗口、终端、shell 开始菜单 -> 运行 -> cmd ...
    锦衣夜行001阅读 1,583评论 0 1