python - MongoEngine throws exception TypeError: cannot deepcopy this pattern object -
i getting exception on querying mongodb model (python/mongoengine) list , regex query. code
from mongoengine import * import re db = connect('testdb') class team(document): name = stringfield() groups = listfield(referencefield('group')) class group(document): name = stringfield() team.drop_collection() group.drop_collection() g1 = group('g1') g1.save() g2 = group('g2') g2.save() g3 = group('g3') g3.save() g4 = group('g4') g4.save() t = team('team1',[g1,g2,g3]) t.save() t = team('team2',[g1,g2,g4]) t.save() t = team('team3',[]) t.save() t = team('team3',[g3,g2]) t.save() t = team('team3',[g4,g1]) t.save() # typeerror: cannot deepcopy pattern object teams = team.objects( q(groups__in=[g3,g2]) & q(name=re.compile('eam3'))) team in teams: print team.name #-------------------------
the exception traceback
traceback (most recent call last): file "so_mongoengine_query.py", line 46, in <module> team in teams: file "/usr/local/lib/python2.7/dist-packages/mongoengine/queryset/queryset.py", line 81, in _iter_results self._populate_cache() file "/usr/local/lib/python2.7/dist-packages/mongoengine/queryset/queryset.py", line 93, in _populate_cache self._result_cache.append(self.next()) file "/usr/local/lib/python2.7/dist-packages/mongoengine/queryset/base.py", line 1137, in next raw_doc = self._cursor.next() file "/usr/local/lib/python2.7/dist-packages/mongoengine/queryset/base.py", line 1182, in _cursor self._cursor_obj = self._collection.find(self._query, file "/usr/local/lib/python2.7/dist-packages/mongoengine/queryset/base.py", line 1215, in _query self._mongo_query = self._query_obj.to_query(self._document) file "/usr/local/lib/python2.7/dist-packages/mongoengine/queryset/visitor.py", line 91, in to_query query = self.accept(simplificationvisitor()) file "/usr/local/lib/python2.7/dist-packages/mongoengine/queryset/visitor.py", line 141, in accept return visitor.visit_combination(self) file "/usr/local/lib/python2.7/dist-packages/mongoengine/queryset/visitor.py", line 41, in visit_combination return q(**self._query_conjunction(queries)) file "/usr/local/lib/python2.7/dist-packages/mongoengine/queryset/visitor.py", line 61, in _query_conjunction combined_query.update(copy.deepcopy(query)) file "/usr/lib/python2.7/copy.py", line 163, in deepcopy y = copier(x, memo) file "/usr/lib/python2.7/copy.py", line 257, in _deepcopy_dict y[deepcopy(key, memo)] = deepcopy(value, memo) file "/usr/lib/python2.7/copy.py", line 174, in deepcopy y = copier(memo) typeerror: cannot deepcopy pattern object
update-1: following code generates same exception
query = ( q(groups__in=[g3,g2]) & q(name=re.compile('eam3'))) copy.deepcopy(query)
any suggestions ? thanks
because deepcopy of compiled regex pattern not supported in python(since python 2.5), i.e.,
copy.deepcopy(re.compile('eam3'))
is not supported. mongoengine use copy.deepcopy when combine multiple query objects. if use single regex filter, query ok mongoengine, not multiples.
mongoengine support set of string queries, may find them in document.
Comments
Post a Comment