Mongoose find returning odd object

2019-02-25 10:38发布

问题:

my current problem is with the db.collection.find() mongoose command. I'm relatively new to mongoose/mongodb, but I've gotten the hang of the concepts of it. Here is the test code I've been trying to run:

mongoose.connect(url);
function main() 
{
    var db = mongoose.connection;
    db.on('open', function() {

        db.collection('Tweet').find({id: 631460910368956400}, function (err, data){
            console.log(data);
        })


        /*var coll = db.collection('Tweet');
         db.collection('Tweet').findOne({id: 631460910368956400},function (err, ret) {
            if(err) console.log(err);
            console.log(ret['id']);

         //db.close();
        });*/
    });
} 
main();

The data returned from the non commented out field is a strange object:

{ connection: null,
  server: null,
  disconnectHandler:
   { s: { storedOps: [], storeOptions: [Object], topology: [Object] },
     length: [Getter] },
  bson: {},
  ns: 'TEST.Tweet',
  cmd: { find: 'TEST.Tweet', limit: 0, skip: 0, query: {}, slaveOk: false },
  options:
   { skip: 0,
     limit: 0,
     raw: undefined,
     hint: null,
     timeout: undefined,
     slaveOk: false,
     db:
      { domain: null,
        _events: [Object],
        _maxListeners: undefined,
        s: [Object],
        serverConfig: [Getter],
         bufferMaxEntries: [Getter],
         databaseName: [Getter],

etc etc... it goes on for much longer.

The IP address is a remote connection that successfully connects. I can do things like add and remove documents, but cannot actually view the documents from the javascript. I know that it is caused due to some kind of asynchronous problem, however I'm not sure how to fix it. Also, the commented out code for .findOne() seems to pull data completely fine in the code above.

What would be the problem with the code for the .find() function? An explanation for why the current error in data retrieving would be great also.

Thanks for your help!

回答1:

The object you receive is a Cursor which is an object used to retrieve the actual results.

When you are sure your query will never return more than one object (like in this case where you query by the always unique _id field), you can use db.collection('Tweet').findOne( which will return just that object without the additional layer of indirection.

But when your query can potentially return more than one document, you need to use a cursor. To resolve the cursor, you can turn it into an array of documents by using cursor.toArray:

    db.collection('Tweet').find({}, function (err, cursor){
        cursor.toArray().forEach(function(doc) { 
            console.log(doc);
        });
    })

This is the most simple version. For more information about cursors, refer to the documentation linked above.

By the way: So far you only used the functionality of the native driver. When you want to use Mongoose to query objects, you might want to use the methods of the Mongoose model object.