node.js - 使用collection.find()无法在mongodb中找到集合的内容

标签 node.js mongodb mongoose

我使用的是 MongoDB shell 版本 v4.0.1 我正在使用 Mongoose 。我有一个叫做品牌的集合。当我尝试将brand.find()记录到控制台时,它返回的不是“品牌”集合的内容,而是:

    Query {
  _mongooseOptions: {},
  mongooseCollection: 
   NativeCollection {
     collection: null,
     opts: 
      { bufferCommands: true,
        capped: false,
        '$wasForceClosed': undefined },
     name: 'offers',
     collectionName: 'offers',
     conn: 
      NativeConnection {
        base: [Object],
        collections: [Object],
        models: [Object],
        config: [Object],
        replica: false,
        options: null,
        otherDbs: [],
        relatedDbs: {},
        states: [Object],
        _readyState: 0,
        _closeCalled: false,
        _hasOpened: false,
        _listening: false },
     queue: [],
     buffer: true,
     emitter: 
      EventEmitter {
        domain: null,
        _events: {},
        _eventsCount: 0,
        _maxListeners: undefined } },
  model: 
   { [Function: model]
     hooks: Kareem { _pres: [Object], _posts: [Object] },
     base: 
      Mongoose {
        connections: [Array],
        models: [Object],
        modelSchemas: [Object],
        options: [Object],
        _pluralize: [Function: pluralize],
        plugins: [Array] },
     modelName: 'offer',
     model: [Function: model],
     db: 
      NativeConnection {
        base: [Object],
        collections: [Object],
        models: [Object],
        config: [Object],
        replica: false,
        options: null,
        otherDbs: [],
        relatedDbs: {},
        states: [Object],
        _readyState: 0,
        _closeCalled: false,
        _hasOpened: false,
        _listening: false },
     discriminators: undefined,
     '$appliedMethods': true,
     '$appliedHooks': true,
     schema: 
      Schema {
        obj: [Object],
        paths: [Object],
        aliases: {},
        subpaths: {},
        virtuals: [Object],
        singleNestedPaths: {},
        nested: {},
        inherits: {},
        callQueue: [],
        _indexes: [],
        methods: {},
        methodOptions: {},
        statics: {},
        tree: [Object],
        query: {},
        childSchemas: [],
        plugins: [Array],
        s: [Object],
        _userProvidedOptions: {},
        options: [Object],
        '$globalPluginsApplied': true },
     collection: 
      NativeCollection {
        collection: null,
        opts: [Object],
        name: 'offers',
        collectionName: 'offers',
        conn: [Object],
        queue: [],
        buffer: true,
        emitter: [Object] },
     Query: { [Function] base: [Object] },
     '$__insertMany': [Function],
     '$init': Promise { <pending> },
     '$caught': true },
  schema: 
   Schema {
     obj: { image: [Function: String], createdAt: [Object] },
     paths: 
      { image: [Object],
        createdAt: [Object],
        _id: [Object],
        __v: [Object] },
     aliases: {},
     subpaths: {},
     virtuals: { id: [Object] },
     singleNestedPaths: {},
     nested: {},
     inherits: {},
     callQueue: [],
     _indexes: [],
     methods: {},
     methodOptions: {},
     statics: {},
     tree: 
      { image: [Function: String],
        createdAt: [Object],
        _id: [Object],
        __v: [Function: Number],
        id: [Object] },
     query: {},
     childSchemas: [],
     plugins: [ [Object], [Object], [Object], [Object], [Object] ],
     s: { hooks: [Object] },
     _userProvidedOptions: {},
     options: 
      { typeKey: 'type',
        id: true,
        noVirtualId: false,
        _id: true,
        noId: false,
        validateBeforeSave: true,
        read: null,
        shardKey: null,
        autoIndex: null,
        minimize: true,
        discriminatorKey: '__t',
        versionKey: '__v',
        capped: false,
        bufferCommands: true,
        strict: true,
        pluralization: true },
     '$globalPluginsApplied': true },
  op: 'find',
  options: {},
  _conditions: {},
  _fields: undefined,
  _update: undefined,
  _path: undefined,
  _distinct: undefined,
  _collection: 
   NodeCollection {
     collection: 
      NativeCollection {
        collection: null,
        opts: [Object],
        name: 'offers',
        collectionName: 'offers',
        conn: [Object],
        queue: [],
        buffer: true,
        emitter: [Object] },
     collectionName: 'offers' },
  _traceFunction: undefined }

其他信息:已成功建立与数据库的连接。控制台显示“连接成功”以响应

MongoClient.connect('mongodb://localhost:27017/testdb', { useNewUrlParser: true }, function(error){
    if(error) console.log(error);

        console.log("connection successful");
});

我定义了品牌常数

const brand = require('./database/models/brand');
console.log(brand.find());

brand.js如下:

const mongoose = require('mongoose')

const brandSchema = new mongoose.Schema({
    name : String,
    image : String
})

const brand = mongoose.model('brand', brandSchema)

module.exports = brand

最佳答案

brand.find() 我只是一个异步操作。你尝试过吗

brand.find()
.then(results => console.log(results))

关于node.js - 使用collection.find()无法在mongodb中找到集合的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52763250/

相关文章:

node.js - 如何通过 Homebrew 软件安装 LTS 版本的 nodejs

javascript - 在表达式中使用最少的字符始终返回真值的最有效方法?

node.js - 如何对 Mongoose 模型进行单元测试?

javascript - 使用 Mongoose 和 Node/ express 保存新帖子时遇到问题

json - 来自 Powershell 的 Mongodb Find() 查询

node.js - 如何使用 Redis 作为缓存系统使用 Node 开发 REST API?

javascript - Node js Mongodb findOne 不同步工作

node.js - 使用 Node 流时内存不足

Mongodb 时间序列操作和生成

node.js - 错误: Connection Closed - MongoDB Node