Mongodb allows regex expression of pattern /pattern/ without using $regex expression.
Mongodb允许正则表达式模式/模式/而不使用$ regex表达式。
http://docs.mongodb.org/manual/reference/operator/query/in/
How can i do it using morphia ?
我如何使用吗啡呢?
If i give Field criteria with field operator as in and value of type "java.util.regex.Pattern" then the equivalent query generated in $in:[$regex: 'given pattern'] which wont return expected results at all.
如果我使用字段运算符作为字段运算符和“java.util.regex.Pattern”类型的值,那么在$ in中生成的等效查询:[$ regex:'given pattern']根本不会返回预期结果。
Expectation: $in :[ /pattern1 here/,/pattern2 here/] Actual using 'Pattern' object : $in : [$regex:/pattern1 here/,$regex:/pattern 2 here/]
期望:$ in:[/ pattern1 here /,/ pattern2 here /]实际使用'Pattern'对象:$ in:[$ regex:/ pattern1 here /,$ regex:/ pattern 2 here /]
2 个解决方案
#1
5
I'm not entirely sure what to make of your code examples, but here's a working Morphia code snippet:
我不完全确定你的代码示例是什么,但这是一个有效的Morphia代码片段:
Pattern regexp = Pattern.compile("^" + email + "$", Pattern.CASE_INSENSITIVE);
mongoDatastore.find(EmployeeEntity.class).filter("email", regexp).get();
Note that this is really slow. It can't use an index and will always require a full collection scan, so avoid it at all cost!
请注意,这真的很慢。它不能使用索引,并且总是需要完整的集合扫描,所以不惜一切代价避免它!
Update: I've added a specific code example. The $in
is not required to search inside an array. Simply use /^I/
as you would in string:
更新:我添加了一个特定的代码示例。 $ in不需要在数组内搜索。只需使用/ ^ I /就像在字符串中一样:
> db.profile.find()
{
"_id": ObjectId("54f3ac3fa63f282f56de64bd"),
"tags": [
"India",
"Australia",
"Indonesia"
]
}
{
"_id": ObjectId("54f3ac4da63f282f56de64be"),
"tags": [
"Island",
"Antigua"
]
}
{
"_id": ObjectId("54f3ac5ca63f282f56de64bf"),
"tags": [
"Spain",
"Mexico"
]
}
{
"_id": ObjectId("54f3ac6da63f282f56de64c0"),
"tags": [
"Israel"
]
}
{
"_id": ObjectId("54f3ad17a63f282f56de64c1"),
"tags": [
"Germany",
"Indonesia"
]
}
{
"_id": ObjectId("54f3ad56a63f282f56de64c2"),
"tags": [
"ireland"
]
}
> db.profile.find({ tags: /^I/ })
{
"_id": ObjectId("54f3ac3fa63f282f56de64bd"),
"tags": [
"India",
"Australia",
"Indonesia"
]
}
{
"_id": ObjectId("54f3ac4da63f282f56de64be"),
"tags": [
"Island",
"Antigua"
]
}
{
"_id": ObjectId("54f3ac6da63f282f56de64c0"),
"tags": [
"Israel"
]
}
{
"_id": ObjectId("54f3ad17a63f282f56de64c1"),
"tags": [
"Germany",
"Indonesia"
]
}
Note: The position in the array makes no difference, but the search is case sensitive. Use /^I/i
if this is not desired or Pattern.CASE_INSENSITIVE
in Java.
注意:数组中的位置没有区别,但搜索区分大小写。如果不需要,请使用/ ^ I / i或Java中使用Pattern.CASE_INSENSITIVE。
#2
1
Single RegEx Filter
单一RegEx过滤器
use .filter()
, .criteria()
, or .field()
使用.filter(),. criteria()或.field()
query.filter("email", Pattern.compile("reg.*exp"));
// or
query.criteria("email").contains("reg.*exp");
// or
query.field("email").contains("reg.*exp");
Morphia converts this into:
Morphia将其转换为:
find({"email": { $regex: "reg.*exp" } })
Multiple RegEx Filters
多个RegEx过滤器
query.or(
query.criteria("email").contains("reg.*exp"),
query.criteria("email").contains("reg.*exp.*2"),
query.criteria("email").contains("reg.*exp.*3")
);
Morphia converts this into:
Morphia将其转换为:
find({"$or" : [
{"email": {"$regex": "reg.*exp"}},
{"email": {"$regex": "reg.*exp.*2"}},
{"email": {"$regex": "reg.*exp.*3"}}
]
})
Unfortunately,
You cannot use $regex operator expressions inside an $in. MongoDB Manual 3.4
你不能在$ in中使用$ regex运算符表达式。 MongoDB手册3.4
Otherwise, we could do:
否则,我们可以这样做:
Pattern[] patterns = new Pattern[] {
Pattern.compile("reg.*exp"),
Pattern.compile("reg.*exp.*2"),
Pattern.compile("reg.*exp.*3"),
};
query.field().in(patterns);
hopefully, one day morphia will support that :)
希望有一天,morphia会支持:)
#1
5
I'm not entirely sure what to make of your code examples, but here's a working Morphia code snippet:
我不完全确定你的代码示例是什么,但这是一个有效的Morphia代码片段:
Pattern regexp = Pattern.compile("^" + email + "$", Pattern.CASE_INSENSITIVE);
mongoDatastore.find(EmployeeEntity.class).filter("email", regexp).get();
Note that this is really slow. It can't use an index and will always require a full collection scan, so avoid it at all cost!
请注意,这真的很慢。它不能使用索引,并且总是需要完整的集合扫描,所以不惜一切代价避免它!
Update: I've added a specific code example. The $in
is not required to search inside an array. Simply use /^I/
as you would in string:
更新:我添加了一个特定的代码示例。 $ in不需要在数组内搜索。只需使用/ ^ I /就像在字符串中一样:
> db.profile.find()
{
"_id": ObjectId("54f3ac3fa63f282f56de64bd"),
"tags": [
"India",
"Australia",
"Indonesia"
]
}
{
"_id": ObjectId("54f3ac4da63f282f56de64be"),
"tags": [
"Island",
"Antigua"
]
}
{
"_id": ObjectId("54f3ac5ca63f282f56de64bf"),
"tags": [
"Spain",
"Mexico"
]
}
{
"_id": ObjectId("54f3ac6da63f282f56de64c0"),
"tags": [
"Israel"
]
}
{
"_id": ObjectId("54f3ad17a63f282f56de64c1"),
"tags": [
"Germany",
"Indonesia"
]
}
{
"_id": ObjectId("54f3ad56a63f282f56de64c2"),
"tags": [
"ireland"
]
}
> db.profile.find({ tags: /^I/ })
{
"_id": ObjectId("54f3ac3fa63f282f56de64bd"),
"tags": [
"India",
"Australia",
"Indonesia"
]
}
{
"_id": ObjectId("54f3ac4da63f282f56de64be"),
"tags": [
"Island",
"Antigua"
]
}
{
"_id": ObjectId("54f3ac6da63f282f56de64c0"),
"tags": [
"Israel"
]
}
{
"_id": ObjectId("54f3ad17a63f282f56de64c1"),
"tags": [
"Germany",
"Indonesia"
]
}
Note: The position in the array makes no difference, but the search is case sensitive. Use /^I/i
if this is not desired or Pattern.CASE_INSENSITIVE
in Java.
注意:数组中的位置没有区别,但搜索区分大小写。如果不需要,请使用/ ^ I / i或Java中使用Pattern.CASE_INSENSITIVE。
#2
1
Single RegEx Filter
单一RegEx过滤器
use .filter()
, .criteria()
, or .field()
使用.filter(),. criteria()或.field()
query.filter("email", Pattern.compile("reg.*exp"));
// or
query.criteria("email").contains("reg.*exp");
// or
query.field("email").contains("reg.*exp");
Morphia converts this into:
Morphia将其转换为:
find({"email": { $regex: "reg.*exp" } })
Multiple RegEx Filters
多个RegEx过滤器
query.or(
query.criteria("email").contains("reg.*exp"),
query.criteria("email").contains("reg.*exp.*2"),
query.criteria("email").contains("reg.*exp.*3")
);
Morphia converts this into:
Morphia将其转换为:
find({"$or" : [
{"email": {"$regex": "reg.*exp"}},
{"email": {"$regex": "reg.*exp.*2"}},
{"email": {"$regex": "reg.*exp.*3"}}
]
})
Unfortunately,
You cannot use $regex operator expressions inside an $in. MongoDB Manual 3.4
你不能在$ in中使用$ regex运算符表达式。 MongoDB手册3.4
Otherwise, we could do:
否则,我们可以这样做:
Pattern[] patterns = new Pattern[] {
Pattern.compile("reg.*exp"),
Pattern.compile("reg.*exp.*2"),
Pattern.compile("reg.*exp.*3"),
};
query.field().in(patterns);
hopefully, one day morphia will support that :)
希望有一天,morphia会支持:)