Mongodb $并没有正常工作

我尝试使用mongodb $和我的nodejs应用程序查找数据,但无法正常工作。

我的代码 –

var brand = req.param('Brand').split(','); brand = '"' + brand.join('","') + '"'; if (query != '') query += ',' query += '{"pro.brand": {$in:['+ brand +']}}' itemdata.find({$and:[query]}, function(err, product) { if (err) { return res.send({info: "Opps sorry"}); } else { res.json({deals: product}); } }); 

当我手动把查询({“pro.brand”:{$ in:[“Micromax”]}}),那么它的工作。 否则,它不工作(它会返回所有的数据)。

查找,或者执行这个函数必须是一个对象。 这里,

 var brand = req.param('Brand').split(','); // Is Array itemdata.find({ $and:[ "pro.band" : { $in : brand // Go into the object } ] }, function(err, product) { if (err) { return res.send({info: "Opps sorry"}); } else { res.json({deals: product}); } }); 

一切都必须是一个对象,并在JavaScript和JSON中的对象具有相同的脚本。 没有必要将其转换为string。

 var brand = req.param('Brand').split(','); // Is Array itemdata.find({ "pro.band" : { $in : brand // Go into the object } }, function(err, product) { if (err) { return res.send({info: "Opps sorry"}); } else { res.json({deals: product}); } }); 

默认情况下是和运营商