Mongoose find returning odd object

前端 未结 1 1728
野的像风
野的像风 2021-01-20 22:05

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 t

相关标签:
1条回答
  • 2021-01-20 22:30

    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.

    0 讨论(0)
提交回复
热议问题