我遇到了以下问题: -
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){.......}
作为参数传递的category
未反映在查询选择中。所以,当我运行上面的代码时,它运行如下: -
`User.find({ _id: userid}, category: 1){.......}`
我的期望是: -
`User.find({ _id: userid}, basicinfo: 1){.......}`
这是什么解决方案?
答案 0 :(得分:2)
var category = req.params.category;
var selection = {};
selection[category] = 1;
User.find({_id:userid}, selection){...}