2023-04-04

4月4日

打印mongoose

const mongoose = require('mongoose') 引入mongoose模塊會(huì)自動(dòng)創(chuàng)建mongoose實(shí)例對(duì)象废赞。

  • mongoose是一個(gè)對(duì)象
  • Mongoose.connections 屬性
  • mongoose.Schema :構(gòu)造函數(shù)
  • Mongoose.model: 函數(shù)
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 的對(duì)象建模工具。

Mongoose 用于和MongoDB 通信(增刪改查)亲澡。

Mongoose運(yùn)行在Node.js 平臺(tái)沥割。

  • Mongoose()是一個(gè)構(gòu)造函數(shù)
  • 引入mongoose模塊會(huì)自動(dòng)創(chuàng)建一個(gè)mongoose實(shí)例對(duì)象耗啦。

mongoose.connect()

定義

mongoose.connect()用于連接MongoDB數(shù)據(jù)庫。

它是一個(gè)異步函數(shù)机杜。

語法

mongoose.connect(uri)
mongoose.connect(數(shù)據(jù)庫連接字符串)

返回值

返回一個(gè)Promise對(duì)象

示例

promise.then()

定義

then()方法為promise綁定回調(diào)函數(shù)帜讲。

語法

promise.then(resolved,rejected)
promise.then((data) => {
  //在異步操作成功時(shí),處理異步操作的結(jié)果
},(error) => {
  //在異步操作失敗時(shí)椒拗,輸出錯(cuò)誤的原因
})

//一個(gè)參數(shù):大多時(shí)候我們更加關(guān)注異步操作成功
promise.then((resolved) => {
  //在異步操作成功時(shí)似将,處理異步操作的結(jié)果
})

//then()方法可以鏈?zhǔn)秸{(diào)用
promise.then((resolved) => {
  //在異步操作成功時(shí),處理異步操作的結(jié)果
})
  .then((resolved) => {})
    .then((resolved) => {})
    .catch(error => {//捕獲錯(cuò)誤信息})

then() 方法最多接受兩個(gè)參數(shù):

  • resolved :Promise 已完成的回調(diào)函數(shù)蚀苛。

  • rejected: Promise已拒絕的回調(diào)函數(shù)在验。

返回值

  • 返回一個(gè)等效的prmose對(duì)象。

示例

promise.catch()

定義

catch()方法用于為promise對(duì)象綁定請(qǐng)求失敗時(shí)的回調(diào)函數(shù)堵未。

catch()方法用于捕獲錯(cuò)誤信息腋舌。

語法

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

返回值

返回一個(gè)promise對(duì)象。

示例

mongoose.model()

定義

用于創(chuàng)建與MongoDB數(shù)據(jù)庫通信的工具渗蟹。

該工具在這里意思就是:指一個(gè)構(gòu)造函數(shù)块饺。

構(gòu)造函數(shù)可以實(shí)例化一個(gè)對(duì)象。

通過實(shí)例對(duì)象的方法與數(shù)據(jù)庫進(jìn)行通信雌芽。

語法

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

返回值

返回大寫的構(gòu)造函數(shù)(Model())

示例

Schema()

定義

Schema()是一個(gè)構(gòu)造函數(shù)/類授艰。

Schema()用于描述數(shù)據(jù)表的結(jié)構(gòu)。

  • 數(shù)據(jù)庫:由多個(gè)數(shù)據(jù)庫表構(gòu)成
  • 數(shù)據(jù)庫表:由多個(gè)document(記錄)構(gòu)成膘怕。
  • Document: 由字段和值構(gòu)成想诅。

語法

 new Schema({定義表結(jié)構(gòu)},{選項(xiàng)對(duì)象})

const blogSchema = new Schema({
  //定義title字段
  title: {
    //字段類型:字符串
    type: String,
    //字段是否必寫: true
    required: true
  },
  body: {
    type: String,
    required: true
  }
}, {})
  • 定義表結(jié)構(gòu): 定義有哪些字段、每個(gè)字段的數(shù)據(jù)類型岛心、是否必填等
  • 選項(xiàng)對(duì)象:提供了一些參數(shù)来破,用來配置第一個(gè)參數(shù)

返回值

返回schema實(shí)例對(duì)象,表示數(shù)據(jù)結(jié)構(gòu)。

示例

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末忘古,一起剝皮案震驚了整個(gè)濱河市徘禁,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌髓堪,老刑警劉巖送朱,帶你破解...
    沈念sama閱讀 217,734評(píng)論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異干旁,居然都是意外死亡驶沼,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,931評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門争群,熙熙樓的掌柜王于貴愁眉苦臉地迎上來回怜,“玉大人,你說我怎么就攤上這事换薄∮裎恚” “怎么了?”我有些...
    開封第一講書人閱讀 164,133評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵轻要,是天一觀的道長(zhǎng)复旬。 經(jīng)常有香客問我,道長(zhǎng)冲泥,這世上最難降的妖魔是什么驹碍? 我笑而不...
    開封第一講書人閱讀 58,532評(píng)論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮凡恍,結(jié)果婚禮上幸冻,老公的妹妹穿的比我還像新娘。我一直安慰自己咳焚,他們只是感情好洽损,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,585評(píng)論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著革半,像睡著了一般碑定。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上又官,一...
    開封第一講書人閱讀 51,462評(píng)論 1 302
  • 那天延刘,我揣著相機(jī)與錄音,去河邊找鬼六敬。 笑死碘赖,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播普泡,決...
    沈念sama閱讀 40,262評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼播掷,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了撼班?” 一聲冷哼從身側(cè)響起歧匈,我...
    開封第一講書人閱讀 39,153評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎砰嘁,沒想到半個(gè)月后件炉,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,587評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡矮湘,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,792評(píng)論 3 336
  • 正文 我和宋清朗相戀三年斟冕,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片缅阳。...
    茶點(diǎn)故事閱讀 39,919評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡磕蛇,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出券时,到底是詐尸還是另有隱情孤里,我是刑警寧澤,帶...
    沈念sama閱讀 35,635評(píng)論 5 345
  • 正文 年R本政府宣布橘洞,位于F島的核電站捌袜,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏炸枣。R本人自食惡果不足惜虏等,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,237評(píng)論 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望适肠。 院中可真熱鬧霍衫,春花似錦、人聲如沸侯养。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,855評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽逛揩。三九已至柠傍,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間辩稽,已是汗流浹背惧笛。 一陣腳步聲響...
    開封第一講書人閱讀 32,983評(píng)論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留逞泄,地道東北人患整。 一個(gè)月前我還...
    沈念sama閱讀 48,048評(píng)論 3 370
  • 正文 我出身青樓拜效,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國和親各谚。 傳聞我的和親對(duì)象是個(gè)殘疾皇子紧憾,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,864評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容

  • 打印mongoose const mongoose = require('mongoose') 引入mongoos...
    天天涯閱讀 71評(píng)論 0 0
  • 首先,我們需要?jiǎng)?chuàng)建一個(gè)nestjs項(xiàng)目: 然后嘲碧,我們需要安裝sqlite3依賴庫: 接下來稻励,我們需要?jiǎng)?chuàng)建一個(gè)use...
    CoderAzreal閱讀 180評(píng)論 0 0
  • 原文地址 本文簡(jiǎn)單的介紹了數(shù)據(jù)庫父阻,以及如何在 Node/Express 中應(yīng)用他們愈涩。之后展示如何使用Mongoos...
    前端幼兒班閱讀 5,166評(píng)論 1 5
  • 使用Node.js開發(fā)App的步驟 創(chuàng)建項(xiàng)目目錄mkdir myappcd myapp# 合并以上兩步驟mkdir...
    勞模閱讀 81評(píng)論 0 0
  • Node.js 第5天課堂筆記 知識(shí)點(diǎn) Express MongoDB 項(xiàng)目一天半的時(shí)間 反饋 新版sublime...
    大佬教我寫程序閱讀 50評(píng)論 0 0