Skip to content

Commit 9b91593

Browse files
author
Johannes Otepka
committed
$regex operator removed and $glob docu added
1 parent f990325 commit 9b91593

File tree

5 files changed

+19
-29
lines changed

5 files changed

+19
-29
lines changed

docs/source/reference/db.md

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,16 +79,22 @@ TaskRecord keys:
7979

8080
MongoDB operators we emulate on all backends:
8181

82-
| Operator | Python equivalent |
83-
| -------- | ----------------- |
84-
| '\$in' | in |
85-
| '\$nin' | not in |
86-
| '\$eq' | == |
87-
| '\$ne' | != |
88-
| '\$gt' | > |
89-
| '\$gte' | >= |
90-
| '\$le' | \< |
91-
| '\$lte' | \<= |
82+
| Operator | Python equivalent |
83+
| -------- |-------------------------------------------------------------------------------|
84+
| '\$in' | in |
85+
| '\$nin' | not in |
86+
| '\$eq' | == |
87+
| '\$ne' | != |
88+
| '\$gt' | > |
89+
| '\$gte' | >= |
90+
| '\$le' | \< |
91+
| '\$lte' | \<= |
92+
| '\$glob' | [fnmatch](https://docs.python.org/3/library/fnmatch.html) (wildcard matching) |
93+
94+
Remarks on _$glob_: The operator can be used to find substrings in DB columns based on
95+
[unix style filename pattern matching](https://docs.python.org/3/library/fnmatch.html)
96+
_$glob_ is **not** a regular MongoDB opertor, but is internally translated to a regular
97+
expression (_$regex_) which is natively supported by MongoDB.
9298

9399
The DB Query is useful for two primary cases:
94100

ipyparallel/controller/dictdb.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@
6262
'$mod': lambda a, b: a % b[0] == b[1],
6363
'$exists': lambda a, b: (b and a is not None) or (a is None and not b),
6464
'$glob': lambda a, b: fnmatch.fnmatch(a, b),
65-
'$regex': lambda a, b: re.match(b, a),
6665
}
6766

6867

ipyparallel/controller/mongodb.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,9 @@ def _translate(self, filter):
125125
raise Exception(
126126
f"unkown paramters {params.keys()} for %glob operator"
127127
)
128-
ret["$regex"] = fnmatch.translate(glob)
128+
# we need to attach the start and end match character to achieve
129+
# an equivalent matching behavior to fnmatch in mongoDB
130+
ret["$regex"] = "^"+fnmatch.translate(glob)+"$"
129131
else:
130132
for key, value in filter.items():
131133
if isinstance(value, dict):

ipyparallel/controller/sqlitedb.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@
4949
# '$mod': None,
5050
# '$exists' : None
5151
'$glob': "GLOB",
52-
'$regex': "REGEXP",
5352
}
5453
null_operators = {
5554
'=': "IS NULL",

ipyparallel/tests/test_db.py

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -149,22 +149,6 @@ def test_find_records_glob(self):
149149
found = [r['msg_id'] for r in recs]
150150
assert set(ref) == set(found)
151151

152-
def test_find_records_regex(self):
153-
"""test finding records with '$regex' operators"""
154-
hist = self.db.get_history()
155-
156-
pattern = "^.+_.$"
157-
ref = [msg_id for msg_id in hist if re.match(pattern, msg_id)]
158-
recs = self.db.find_records({'msg_id': {'$regex': pattern}}, ["msg_id"])
159-
found = [r['msg_id'] for r in recs]
160-
assert set(ref) == set(found)
161-
162-
pattern = "^.+_1[0-9]$"
163-
ref = [msg_id for msg_id in hist if re.match(pattern, msg_id)]
164-
recs = self.db.find_records({'msg_id': {'$regex': pattern}}, ["msg_id"])
165-
found = [r['msg_id'] for r in recs]
166-
assert set(ref) == set(found)
167-
168152
def test_get_history(self):
169153
msg_ids = self.db.get_history()
170154
latest = datetime(1984, 1, 1).replace(tzinfo=utc)

0 commit comments

Comments
 (0)