File tree Expand file tree Collapse file tree 5 files changed +19
-29
lines changed
Expand file tree Collapse file tree 5 files changed +19
-29
lines changed Original file line number Diff line number Diff line change @@ -79,16 +79,22 @@ TaskRecord keys:
7979
8080MongoDB 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
9399The DB Query is useful for two primary cases:
94100
Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff 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 ):
Original file line number Diff line number Diff line change 4949 # '$mod': None,
5050 # '$exists' : None
5151 '$glob' : "GLOB" ,
52- '$regex' : "REGEXP" ,
5352}
5453null_operators = {
5554 '=' : "IS NULL" ,
Original file line number Diff line number Diff 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 )
You can’t perform that action at this time.
0 commit comments