问题
I know I can do a glob-type search on mongodb:
db.person.find({ name: /*.bob.*/ })
or
db.person.find({ name: { $regex: '*.bob.*' }})
How do I do this with mongoengine without using a raw query (which is apparently the only way based on my searches)?
I've blindly tried several variations like:
Person.objects(name='/.*bob.*/')
Person.objects(name='/\.*bob\.*/')
Person.objects(name='.*bob.*')
Person.objects(name='\\.*bob\\.*')
etc, to no avail...
回答1:
It looks like you can do it this way:
import re
regex = re.compile('.*bob.*')
Person.objects(name=regex)
来源:https://stackoverflow.com/questions/39090178/python-mongoengine-do-like-regex-search