python, mongoengine - do like/regex search

ⅰ亾dé卋堺 提交于 2019-12-07 02:21:42

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!