nodejs get find results in mongodb

前端 未结 2 1682
南笙
南笙 2021-01-07 06:02

I\'m trying to get the result of query but i get the same info in all vars: db, collection and res:

var mongodb = require(\"mongodb\");
var mongoserver = new         


        
相关标签:
2条回答
  • 2021-01-07 06:31

    You can use this:

     collection.find().toArray(function(err, docs){
                    console.log(docs);
        )};
    
    0 讨论(0)
  • 2021-01-07 06:40

    .find() will return a Cursor object for you to work with. If all you are interested in is getting all the results in an array you can do:

    collection.find().toArray(function(err, docs) {
        console.log(docs);
    });
    

    But you can also iterate the cursor too:

    collection.find().each(function(err, doc) {
        //called once for each doc returned
    });
    
    0 讨论(0)
提交回复
热议问题