Details
-
Bug
-
Resolution: Duplicate
-
Major - P3
-
None
-
3.6.23, 4.2.17
-
None
-
None
-
ALL
Description
TheĀ $regex operation accepts invalid expressions when used inside an aggregate command. For example, the regular expression ^+somestring is not valid. However, Mongo executes the following command without any error:
{
|
"aggregate": "PayeeReputationByPayeeAndLegalEntity",
|
"pipeline": [
|
{
|
"$match": {
|
"_id": {
|
"$regex": "^+somestring",
|
"$options": ""
|
}
|
}
|
}
|
],
|
"explain": true
|
}
|
The problem is that the query plan for the above aggregation is the following:
{
|
"waitedMS": NumberLong(0),
|
"stages": [
|
{
|
"$cursor": {
|
"query": {
|
"_id": {
|
"$regex": "^+somestring",
|
"$options": ""
|
}
|
},
|
"queryPlanner": {
|
"plannerVersion": 1,
|
"namespace": "db.some-collection"
|
"indexFilterSet": false,
|
"parsedQuery": {
|
"_id": /^+somestring/
|
},
|
"winningPlan": {
|
"stage": "FETCH",
|
"inputStage": {
|
"stage": "IXSCAN",
|
"filter": {
|
"_id": /^+somestring/
|
},
|
"keyPattern": {
|
"_id": 1
|
},
|
"indexName": "_id_",
|
"isMultiKey": false,
|
"isUnique": true,
|
"isSparse": false,
|
"isPartial": false,
|
"indexVersion": 1,
|
"direction": "forward",
|
"indexBounds": {
|
"_id": [
|
"[\"\", {})",
|
"[/^+somestring/, /^+somestring/]"
|
]
|
}
|
}
|
},
|
"rejectedPlans": []
|
}
|
}
|
}
|
],
|
"ok": 1.0
|
}
|
As we can notice, the indexBounds information is completely nonsense:
"indexBounds": {
|
"_id": [
|
"[\"\", {})",
|
"[/^+somestring/, /^+somestring/]"
|
]
|
}
|
The above bound makes the database perform a full index-scan. Adding an escape character in front of the + character doesn't change the situation. So, the expression ^+somestring produces the same effects.
The real problem is that the above command is the command generated by Spring Data MongoDB. In this case, there is no workaround ![]()
I didn't try to reproduce it on version 5.x of Mongo, but I'm quite sure that the bug persists.
Attachments
Issue Links
- duplicates
-
SERVER-39698 Make $regexFind, $regexFindAll, and $regexMatch error if the regex is invalid
-
- Closed
-