2016-04-29 36 views
1

执行db.collection.find后,Req.params将获取值。可以有人告诉我我在做什么这个代码错了吗?无法将参数传递给mongo查找集合

exports.findAll = function(req, res) { 
    var postal = parseInt(req.params.postal); 
    db.collection('ifscdata', function(err, collection) { 
     collection.find({'ADDRESS':/postal/}).toArray(function(err, items) { 
      res.send(items); 
     }); 
    }); 

上午应该根据邮件做地址部分搜索。但是因为它只有在获得价值之后才能将价值转嫁给邮政。

函数路径是这样的

app.get('/ifsc/:postal', ifsc.findAll); 

样品网址:

http://localhost:3000/ifsc/691009

回答

1

看起来你需要你的查询中使用正则表达式,考虑与RegExp对象包裹变量,如下所示:

exports.findAll = function(req, res) { 
    var postal = req.params.postal, 
     regex = new RegExp(postal); 

    db.collection('ifscdata', function(err, collection) { 
     collection.find({'ADDRESS': regex}).toArray(function(err, items) { 
      res.send(items); 
     }); 
    }); 
+1

谢谢:)而且很快:P – user2038580

0

为什么你把postal之间的斜线?

你试过这个吗?

exports.findAll = function(req, res) { 
    var postal = parseInt(req.params.postal); 
    db.collection('ifscdata', function(err, collection) { 
     collection.find({'ADDRESS': postal}).toArray(function(err, items) { 
      res.send(items); 
     }); 
    }); 
+0

它像MongoDB中的部分比较。它检查价值的至少一部分是否在现场。 – user2038580