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

Running code when mongoengine loads a document
Can't replace mongo document
How do I call a function with pymongo using webpy?
python motor offers fetch_next exception on aggregation
python motor mongo cursor length or peek next
PyMongo check if update succeeded
insert and query an OrderedDict in MongoHQ
Return .str of ObjectID using pymongo
Selecting the lowest/highest with find_one in Pymongo
Safe use of Pymongo's find_one
Pymongo $in + $regex
How can I get pymongo to always return str and not unicode?
SON Manipulator in mongo queries
How to append a document to a 2nd level embedded model list document in Django and mongoengine
pymongo multiple update $in
How to get negetive of a complex where clause in mongo db

Categories

HOME
spring-data
events
yaml
ontouchlistener
seedstack
workflow
swi-prolog
antd
plaid
openlayers-3
malware
biztalk-2010
position
telegraf
hystrix
csrf
spring-amqp
amazon-elb
trace32
asp.net-core-1.0
twisted
gravity
tfsbuild
raml
wsf
bluemix-mobile-services
opam
liferay-6.2
cell
fusetools
pubxml
crud
android-cardview
gravity-forms-plugin
sfml
fatfs
openbr
nest
spring-rabbitmq
express-session
jfxtras
formsauthenticationticket
runtimeexception
imgur
python-jira
grails3.2.0
hdiv
julius-speech
permission-denied
windows-media-player
scala-breeze
jgrapht
visual-format-language
algebra
portability
stateless-session-bean
emf-compare
annotatorjs
petsc
allegro
cocoa-bindings
libharu
clp
django-testing
nsdata
static-methods
wicked-pdf
apache-pig-grunt
urbit
polymerfire
boost-proto
miniconda
openshift-cartridge
astropy
ora-04091
blockquote
uitest
web-api
htmltidy
findersync
pass-by-value
thoughtworks-go
encryption-symmetric
proxy-server
colon
non-ascii-characters
disjoint-union
apportable
microformats
axacropdf
smartfox
onconfigurationchanged
zotonic
kobold2d
firebird1.5
opends
n-queens
representation
fork-join
servlet-container
printqueue
android-2.1-eclair
eventaggregator
html-components
processors
source-code-protection
wmd-editor
fuzzy-comparison
prism-2
rendering-engine
webclient.uploaddata

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App