PyMongo $ in + $ regex [英] PyMongo $in + $regex
本文介绍了PyMongo $ in + $ regex的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在PyMongo中将$ regex与$ in结合在一起?
How can I combine $regex with $in in PyMongo?
我想搜索/*.heavy.*/
或/*.metal.*/
.
我在python中尝试失败,
I tried in python without success:
db.col.find({'music_description' : { '$in' : [ {'$regex':'/*.heavy.*/'} ]} })
Mongo shell中的等效项是:
The equivalent in Mongo shell is:
db.inventory.find( { music_description: { $in: [ /heavy/, /metal/ ] } } )
推荐答案
使用python正则表达式.
Use python regular expressions.
import re
db.col.find({'music_description': {'$in': [ re.compile('.*heavy.*'), re.compile('.*metal.*')]}})
这篇关于PyMongo $ in + $ regex的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文