使用collection.find()在mongodb中找不到集合的内容

时间:2018-10-11 14:58:47

标签: node.js mongodb mongoose

我正在使用MongoDB Shell版本v4.0.1 我在用猫鼬。我有一个名为brand的系列。当我尝试将brand.find()登录到控制台时,而不是返回'brand'集合的内容,而是返回以下内容:

    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

1 个答案:

答案 0 :(得分:0)

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

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