简体   繁体   中英

Cannot find contents of the collection in mongodb using collection.find()

I am using MongoDB shell version v4.0.1 I am using mongoose. I have a collection called brand. When I try to log brand.find() to the console, instead of returning the contents of 'brand' collection, it's returning this :

    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 }

Additional information : Connection to database is successfully established. The console says "connection successful" in response to

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

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

I have defined brand constant

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

brand.js is as follows :

const mongoose = require('mongoose')

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

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

module.exports = brand

brand.find() i just an async operation. Did you try

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM