pymongo


Pymongo $in + $regex


How can I combine $regex with $in in pymongo?
I want to search for either /*.heavy.*/ or /*.metal.*/.
I tried in python without success:
db.col.find({'music_description' : { '$in' : [ {'$regex':'/*.heavy.*/'} ]} })
The equivelant in mongo shell is:
db.inventory.find( { music_description: { $in: [ /heavy/, /metal/ ] } } )
Use python regular expressions.
import re
db.col.find({'music_description': {'$in': [ re.compile('.*heavy.*'), re.compile('.*metal.*')]}})
Why even bother using an $in?
You're wasting processing by evaluating the field for each value within the list, and since each value is a regex it has its own performance considerations,
Depending on how long your query strings get, it might be prudent just to wrap them up in one regex and avoid the $in query all together
import re
db.col.find({'music_description': re.compile('heavy|metal')})
similarly in mongo shell
db.inventory.find({music_description: /heavy|metal/})
as for [user2998367]'s answer, you're wasting efficiency compiling a regex with greedy wildcards for the sole purpose of a match, the difference between re.search and re.match in python requires the use of the wildcards for re.search purposes, but re.match behaves as 'anywhere in string', as does MongoDB, its only really needed if you're intending to extract, which you'd need to do later after querying anyhow, or if you're reusing a compiled regex somewhere else that you specifically need re.search over re.match

Related Links

Pymongo error : 'Cursor' object has no attribute 'find' , how to filter it?
Spark Streaming: foreachRDD insert into mongoDB using python?
how to find right version of bson from pip for pymongo/mongoengine
How to enable `projection` in pymongo query class `find()`?
Variables don't hold value for very long pymongo ipython
pymongo upgrade to 3.0.3 causes this statement to fail
UUID to NUUID in Python
how to test for empty mongodb query in pymong
read video file from mongodb with pymongo
Get OperationFailure error with PyMongo 2.7.1
Inserting string as regular string in mongodb
Mongos + Pymongo 2.5 ==>No suitable hosts found
pymongo collection add item using update
How to convert objectid to string
Conditional atomic mongo remove with different results
python script to connect to mongodb master

Categories

HOME
protocol-buffers
windows-8.1
openxml
key
website
lvm
azure-data-lake
google-search-console
joomla3.2
swarm
cqrs
alljoyn
mpu6050
smartgwt
sql-injection
sonicwall
rpmbuild
nullreferenceexception
infrared
uislider
fluent-migrator
web-config-transform
multi-dimensional-scaling
ccavenue
sparkle
mdns
matlab-app-designer
phpstorm-2016.1
series
riotjs
listjs
mongoengine
buildforge
xerces-c
jslint
macports
microsoft-ui-automation
utf
hdiv
access-denied
vesta
punctuation
bpms
ndk-build
fragmentstatepageradapter
declarative
powerpoint-2013
apache-pig-grunt
gridbaglayout
flipclock
gdl
variable-length-array
knife
vdm-sl
alwayson
ocmockito
pass-by-value
uno
cout
global-scope
zend-mail
loginview
squeezebox
two.js
senchatouch-2.4
windows-taskbar
django-settings
boinc
xalan
affinetransform
dynamic-c
motodev-studio
cewolf
directshow.net
digiflow
jmenu
dip
formal-semantics
android-input-method
eventaggregator
objectinstantiation

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App