MongoAlchemy 正则表达式 return 无

MongoAlchemy regex return nothing

我正在为一个项目测试 MongoAlchemy,我必须按名称搜索用户。

我正在尝试制作正则表达式,但查询结果始终为空。

我尝试了两种方法:

import re
users = User.query.filter({"name":re.compile("/a/", re.IGNORECASE)}).all()

并且:

users = User.query.filter(User.name.regex('/a/', ignore_case=True)).all()

即使我使用像 /.*/ 这样非常通用的正则表达式,结果也总是空的。

谢谢。

在 python regular expressions 中未使用 /regexp/ 定义,这是 javascript 语法。

初始化正则表达式的正确方法是:

re.compile(r".*", re.IGNORECASE)

所以你应该使用:

users = User.query.filter({"name": re.compile(r".*", re.IGNORECASE)}).all()