[英]Mongoose find query doesn't work even equivalent query works fine on mongodb
I'm implementing a tag system in my page. 我正在我的页面中实现标签系统。 When user entry any string it will trigger my express route and try to find the content typed to fill a list and return it as JSON to process the callback.
当用户输入任何字符串时,它将触发我的快速路由并尝试查找键入的内容以填充列表并将其作为JSON返回以处理回调。 The problem is about moongose, because the query is pretty right and works fine if I execute it on mongo shell.
问题是关于moongose,因为查询非常正确,如果我在mongo shell上执行它,它的工作正常。
The query: 查询:
var token = '/.*' + req.query.search + '.*/i';
Tag.find({ description: token , inactivatedAt: null }, function(err, tags) {
var tempArray = [];
if (tags) {
var counter = 0;
tags.forEach(function(tag) {
var dArray = [];
dArray.push(counter++);
dArray.push(tag.description);
dArray.push(null);
dArray.push(null);
tempArray.push(dArray);
});
}
res.writeHead(200, {'content-type': 'text/json'});
res.end(JSON.stringify(tempArray));
});
Even if I query as description: { $regex: token }
or description: token
the result is not found. 即使我查询
description: { $regex: token }
或description: token
,也找不到结果。 The same query parameters are fine and brings result from the mongo on the shell. 相同的查询参数很好,并在shell上带来了mongo的结果。 Eg:
db.tags.find({ description: /.*MET.*/i , inactivatedAt: null }).pretty()
but my mongoose query doesn't work. 例如:
db.tags.find({ description: /.*MET.*/i , inactivatedAt: null }).pretty()
但我的mongoose查询不起作用。
Any clue will be very helpful. 任何线索都会非常有帮助。 Thanks in advance!
提前致谢!
您需要将token
转换为常规表达而不仅仅是字符串:
var token = new RegExp(req.query.search, 'i');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.