MongoJS在使用正则expression式search时不返回数据

Node.js, mongojs ,mongodb。 我正在使用正则expression式search技能列表。 这是我的服务器代码:

var nconf = require('nconf'); var db = require('mongojs').connect(nconf.get('mongolab:connection'), ['skills', 'users']); app.get('/api/skill', function(req, res){ console.log('searching for ' + req.query['q']); var query = '{name: /' + req.query['q'] + '/i}'; console.log('query: ' + query); db.skills.find(query, function(err, data){ console.log('returning ' + JSON.stringify(data)); if(!err){ res.writeHead(200, {'content-type': 'text/json' }); res.write( JSON.stringify(data) ); res.end('\n'); } }); }); 

我的列表中有一个“asp.net”的值。 控制台日志输出:

 searching for .net query: {name: /.net/i} returning [] 

我使用MongoHub连接到相同的服务器/数据库,粘贴查询字段中的语句,并取回我的logging:

 { "name": "asp.net", "description": "", "_id": { "$oid": "500b4aae14f7960e91000001" } } 

有什么build议么?

query必须是一个对象,而不是一个string。 试试这个:

 var query = {name: new RegExp(req.query['q'], 'i') };