4

I would like to count documents with a certain amount of characters for one specific field, and I do this by using regex:

total_count = db.collection.count({'field': {'$regex': '^pattern{m,n}$'}})

This fails. The problem is in the {m,n} syntax for mongodb/pymongo, because the following gives good results:

total_count = db.collection.count({'field': {'$regex' : '^pattern+'}})

and the expression '/^pattern{m,n}$/' works smoothly in other applications (tested on: http://www.regexr.com/)

In my case, pattern = [0-9a-zA-Z \W], but this should not be relevant.

styvane
  • 59,869
  • 19
  • 150
  • 156
annakeuchenius
  • 195
  • 2
  • 8

1 Answers1

2

You need to compile your regular expression pattern using re.compile function into a regular expression object

import re
pat = re.compile(r'^pattern{n, m}$')
total_count = db.collection.count({'field': {'$regex': pat}})
styvane
  • 59,869
  • 19
  • 150
  • 156
  • wow thanks! That actually works. Though I find it surprising as I thought mongodb had a regex compiler build in so I thought it would be unnecessary double/repeating.. I thougt recompiling upfront (in python) was only necessary in case of using 'options' of regex...Could you elaborate as to why your answer works? http://stackoverflow.com/questions/3483318/performing-regex-queries-with-pymongo – annakeuchenius Oct 30 '15 at 11:05
  • @annakeuchenius read the first comment to the accepted answer and the second answer. – styvane Oct 30 '15 at 11:06