-->

python, mongoengine - do like/regex search

2019-05-04 20:18发布

问题:

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)