如何使用Mongoose 7.3.1打印数据库文档

sxissh06  于 2023-06-30  发布在  Go
关注(0)|答案(1)|浏览(111)

我目前正在使用Mongoose 7.3.1,并已将一些文档插入MongoDB数据库,但我无法使用console.log()记录文档,只需使用Fruit.find({})并将其记录到控制台,它所吐出的是一个不需要的对象的巨大数据集。
不需要的输出如下

Query {
  _mongooseOptions: {},
  _transforms: [],
  _hooks: Kareem { _pres: Map(0) {}, _posts: Map(0) {} },
  _executionStack: null,
  mongooseCollection: Collection {
    collection: null,
    Promise: [Function: Promise],
    modelName: 'Fruit',
    _closed: false,
    opts: {
      autoIndex: true,
      autoCreate: true,
      schemaUserProvidedOptions: {},
      capped: false,
      Promise: undefined,
      '$wasForceClosed': undefined
    },
    name: 'fruits',
    collectionName: 'fruits',
    conn: NativeConnection {
      base: [Mongoose],
      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: [Array],
      _listening: false,
      _connectionOptions: [Object],
      _connectionString: 'mongodb://localhost:27017/fruitsDB',
      client: [MongoClient],
      '$initialConnection': [Promise]
    },
    queue: [],
    buffer: true,
    emitter: EventEmitter {
      _events: [Object: null prototype] {},
      _eventsCount: 0,
      _maxListeners: undefined,
      [Symbol(kCapture)]: false
    }
  },
  model: Model { Fruit },
  schema: Schema {
    obj: {
      name: [Function: String],
      rating: [Function: Number],
      review: [Function: String]
    },
    paths: {
      name: [SchemaString],
      rating: [SchemaNumber],
      review: [SchemaString],
      _id: [ObjectId],
      __v: [SchemaNumber]
    },
    aliases: {},
    subpaths: {},
    virtuals: { id: [VirtualType] },
    singleNestedPaths: {},
    nested: {},
    inherits: {},
    callQueue: [],
    _indexes: [],
    methods: {},
    methodOptions: {},
    statics: {},
    tree: {
      name: [Function: String],
      rating: [Function: Number],
      review: [Function: String],
      _id: [Object],
      __v: [Function: Number],
      id: [VirtualType]
    },
    query: {},
    childSchemas: [],
    plugins: [ [Object], [Object], [Object], [Object], [Object] ],
    '$id': 1,
    mapPaths: [],
    s: { hooks: [Kareem] },
    _userProvidedOptions: {},
    options: {
      typeKey: 'type',
      id: true,
      _id: true,
      validateModifiedOnly: false,
      validateBeforeSave: true,
      read: null,
      shardKey: null,
      discriminatorKey: '__t',
      autoIndex: null,
      minimize: true,
      optimisticConcurrency: false,
      versionKey: '__v',
      capped: false,
      bufferCommands: true,
      strictQuery: false,
      strict: true,
      pluralization: true
    },
    '$globalPluginsApplied': true,
    _requiredpaths: []
  },
  op: 'find',
  options: {},
  _conditions: {},
  _fields: undefined,
  _updateDoc: undefined,
  _path: undefined,
  _distinctDoc: undefined,
  _collection: NodeCollection {
    collection: Collection {
      collection: null,
      Promise: [Function: Promise],
      modelName: 'Fruit',
      _closed: false,
      opts: [Object],
      name: 'fruits',
      collectionName: 'fruits',
      conn: [NativeConnection],
      queue: [],
      buffer: true,
      emitter: [EventEmitter]
    },
    collectionName: 'fruits'
  },
  _traceFunction: undefined,
  '$useProjection': true
}

我甚至尝试检查数据库中文档的对象,但无法找到任何对象。
Mongoose官方文档here提到使用上面的代码来查找文档,但它没有显示文档。
如何使用Mongoose将文档登录到终端?

a64a0gku

a64a0gku1#

您遇到的问题是由于数据库调用的异步特性造成的。Mongoose的find方法返回一个Query对象,它的作用类似于一个promise,您需要相应地处理它。您可以尝试使用.then()或async/await来处理它。类似于这个的东西:

Fruit.find({}).then((docs) => {
    console.log(docs);
}).catch((err) => {
    console.error(err);
});

相关问题