Acquiring all documents within a collection through Node.js and mongoDB

Feedback


Question:

I require assistance with fetching all data from a collection by utilizing MongoDb and Node.js. My code is explained below.

exports.readProfileData=function(req,res){
    db.profile.findOne({
        colgid:1
    },{
        colgname:1,
        colgSname:1,
        address:1,
        contno:1,
        email:1
    },function(err,docs){
        if(!err){
            if(docs){
                res.send(docs);
            }
        }
    });
}

I have code that retrieves a single document from the profile collection. However, I want to retrieve all documents from the profile collection and send them to the client side. Could you assist me with this?


Solution:

Employing

find({})

will capture everything.

exports.readProfileData=function(req,res){
    db.profile.find({},{
        colgname:1,
        colgSname:1,
        address:1,
        contno:1,
        email:1
    },function(err,docs){
        if(!err){
            if(docs){
                res.send(docs);
            }
        }
    });
}

Frequently Asked Questions