I am trying to run a query on mongodb with mongoose when accessing my API, however this doesn't work. It always sends me an error.
我正在尝试在访问我的API时使用mongoose在mongodb上运行查询,但是这不起作用。它总是给我一个错误。
If I search on mongo client with db.my_collection.find({ingredient_name: ["olive","oil"]}) then it works perfectly.
如果我使用db.my_collection.find({ingredient_name:[“olive”,“oil”]})搜索mongo客户端,那么它可以完美地工作。
my code looks like below:
我的代码如下所示:
exports.show = function(req, res) {
var tmp = req.params.id.split(",");
var search = JSON.stringify(tmp);
console.log("searching for "+search);
ComplementaryFood.find({
ingredient_name : { $in : search }
} ,function (err, complementary_food) {
if(err) { return handleError(res, err); }
if(!complementary_food) { return res.send(404); }
return res.json(complementary_food);
});
};
search is a JSON like ["olive","oil"] or ["beer"], etc.
搜索是一种JSON,如[“橄榄”,“油”]或[“啤酒”]等。
Please help, I've already read the moongose documentation, tried a lot of different things and none worked ...
请帮助,我已经阅读了moongose文档,尝试了很多不同的东西,但都没有工作......
btw. my project is built upon angular-fullstack-generator from yoman
顺便说一句。我的项目建立在yoman的angular-fullstack-generator之上
1 个解决方案
#1
2
$in
takes an array value, not a string. So don't stringify tmp
, just use it directly:
$ in采用数组值,而不是字符串。所以不要将tmp字符串化,只需直接使用它:
ComplementaryFood.find({
ingredient_name : { $in : tmp }
}, ...
#1
2
$in
takes an array value, not a string. So don't stringify tmp
, just use it directly:
$ in采用数组值,而不是字符串。所以不要将tmp字符串化,只需直接使用它:
ComplementaryFood.find({
ingredient_name : { $in : tmp }
}, ...