在 MongoDB 文档中搜索带有特殊字符的字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16560291/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Searching string with special characters in MongoDB document
提问by dex
I want to search values having special characters such as " $ / . @ > "
in a document.
我想搜索具有特殊字符的值,例如" $ / . @ > "
文档中的值。
Lets consider, I've myKey with values like "test$australia", "test$austria", "test$belgium", "green.africa".
让我们考虑一下,我的 myKey 具有类似的值 "test$australia", "test$austria", "test$belgium", "green.africa".
I want to search values with '.*$aus.*',
我想用 '.*$aus.*',
For example,
例如,
db.myCollection.find({ myKey : /.*$aus.*/i });
OR
或者
db.myCollection.find({ myKey : { '$regex' : '.*$aus.*','$options' : 'i' });
Above queries dont work , how should I form query? I'm using MongoDB 2.4.1.
以上查询不起作用,我应该如何形成查询?我正在使用 MongoDB 2.4.1。
采纳答案by Stephane Godbillon
You have to escape $
by \
:
您必须$
通过\
以下方式逃脱:
db.myCollection.find({ myKey : /.*$aus.*/i });
// OR
db.myCollection.find({myKey: { $regex: '.*\$aus.*', $options: 'i'}})
回答by Jaj
db.test.insert({word: 'hai('});
db.test.insert({word: 'jana'});
Output be like that :
输出是这样的:
{ "_id" : ObjectId("56f27fb71d843581261433c6"), "word" : "hai(" }
{ "_id" : ObjectId("56f27ffe1d843581261433c8"), "word" : "jana" }
Note : Want the special char row alone so
注意:所以想要单独的特殊字符行
db.test.find({word:{$regex:"\("}});
Output be like this:
输出是这样的:
{ "_id" : ObjectId("56f27fb71d843581261433c6"), "word" : "hai(" }
回答by Arjun G Perambra
You can use this:
你可以使用这个:
db.myCollection.find({myKey:{ $regex:new RegExp('^' + 'test$australia'.replace(/[-\/\^$*+?.()|[\]{}]/g, '\$&') + '$', 'i')}})
回答by Ilarion Halushka
Escape all regex special characters:
转义所有正则表达式特殊字符:
req.query.name.replace(/[.*+?^${}()|[\]\]/g, '\$&');
Create a query with regex and option "i" (ignore case):
使用正则表达式和选项“i”(忽略大小写)创建查询:
const databaseQuery.name = new RegExp(`${req.query.name}`, 'i');
Perform a search with a query:
使用查询执行搜索:
db.collection.find(databaseQuery)
Note: don't forget to create indexes for the fields that you will search through. Indexing fields increases the speed of your regex queries. In my case for my 'name' field it would be like this:
注意:不要忘记为要搜索的字段创建索引。索引字段提高了正则表达式查询的速度。就我的“姓名”字段而言,它会是这样的:
db.collection.createIndex({ name: "text" })
回答by Falcon Ryu
You can use https://www.npmjs.com/package/regex-escape. It is a good library for escaping special characters to be used in regular expressions
您可以使用https://www.npmjs.com/package/regex-escape。这是一个很好的转义特殊字符的库,用于正则表达式
var RegexEscape = require("regex-escape");
let keyword = RegexEscape("{#/}");
// => \{#\/\}
db.myCollection.find({ myKey : { '$regex' : keyword, '$options' : 'mi' });
回答by Anson Alexander Cardoz
Define the search key under the re.escape
function that will resolve the issue for special character regex.
在re.escape
将解决特殊字符正则表达式问题的函数下定义搜索键。
{search_field: {"$regex": '{}.*'.format(re.escape(search_key)),"$options": 'i'}}