Question

I am stuck in the following issue :-

var userid = req.params.userid;
var category = req.params.category; 
// category can be basicinfo, address, contactinfo, etc

var selection = {
        category: 1
    };

User.find({ _id: userid}, selection){.......}

The category which is passed as parameter doesn't reflect in the query selection. so, when i run the above code it runs like this: -

`User.find({ _id: userid}, category: 1){.......}`

What i expect is :-

`User.find({ _id: userid}, basicinfo: 1){.......}`

What is solution for this?

Was it helpful?

Solution

var category = req.params.category;
var selection = {};
selection[category] = 1;
User.find({_id:userid}, selection){...}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top