我在使用list和regex查询查询mongodb模型(Python / MongoEngine)时遇到异常。代码是
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 this pattern object
teams = Team.objects( Q(groups__in=[g3,g2]) & Q(name=re.compile('eam3')))
for team in teams:
print team.name
#-------------------------
异常回溯是
Traceback (most recent call last):
File "so_mongoengine_query.py", line 46, in <module>
for 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 this pattern object
Update-1:以下代码生成相同的异常
query = ( Q(groups__in=[g3,g2]) & Q(name=re.compile('eam3')))
copy.deepcopy(query)
有什么建议吗? 感谢
答案 0 :(得分:6)
因为Python(since Python 2.5)不支持编译的正则表达式模式的深度复制,即
copy.deepcopy(re.compile('eam3'))
不受支持。当组合多个查询对象时,mongoengine将使用copy.deepcopy。因此,如果您使用单个正则表达式过滤器,则使用mongoengine查询是正常的,但不是倍数。
mongoengine支持一组字符串查询,您可以找到它们in the document。
答案 1 :(得分:1)
使用 mongoengine==0.22.1 我有一个这样的查询:
regex = re.compile(".*v3\.[0-9]{1,4}/transaction/[0-9a-fA-F]*$")
from_date = kwargs.get("from_date", "2000-01-01")
to_date = kwargs.get("to_date", (datetime.utcnow() + timedelta(days=1)).strftime("%Y-%m-%d"))
qs = (
ApiLog.objects.filter(
time_stamp__gte=parse(from_date),
time_stamp__lt=parse(to_date) + timedelta(days=1),
)
.filter(url=regex)
.only("id", "url")
.order_by("time_stamp")
)
这个查询产生了可怕的
"TypeError: cannot deepcopy this pattern object"
当我尝试对结果进行分页时。
从 python 3.6.12 升级到 3.7.9 为我解决了这个问题。
我不确定为什么会发生这个错误,也不知道它的起源是否与 OP 共享的相同,但由于此页面是在谷歌搜索中出现的此错误,我想我会在这里发布。