Performing regex Queries with pymongo

mongodb regex
mongodb regex not
mongodb string replace regex
mongoose regex
mongodb regex search on integer value
mongodb regex digit
regex in mongodb c#
mongodb regex not working

I am trying to perform a regex query using pymongo against a mongodb server. The document structure is as follows

{
  "files": [
    "File 1",
    "File 2",
    "File 3",
    "File 4"
  ],
  "rootFolder": "/Location/Of/Files"
}

I want to get all the files that match the pattern *File. I tried doing this as such

db.collectionName.find({'files':'/^File/'})

Yet i get nothing back , am i missing something because according to the mongodb docs this should be possible. If I perform the query in the mongo console it works fine , does this mean the api doesnt support it or am I just using it incorrectly

If you want to include regular expression options (such as ignore case), try this:

import re
regx = re.compile("^foo", re.IGNORECASE)
db.users.find_one({"files": regx})

Performing regex Queries with pymongo, If you want to include regular expression options (such as ignore case), try this: import re regx = re.compile("^foo", re.IGNORECASE)  PyMongo is a Python distribution containing tools for working with MongoDB. To perform regex queries with PyMongo, the syntax is as follows −db.yourCollection

Turns out regex searches are done a little differently in pymongo but is just as easy.

Regex is done as follows :

db.collectionname.find({'files':{'$regex':'^File'}})

This will match all documents that have a files property that has a item within that starts with File

How to Query MongoDB Documents with Regex in Python , When you do a method call using the "$regex" operator for PyMongo, you need to pass a query dictionary with another dictionary nested inside it. One of the keys inside the nested “inner” dictionary must be "$regex" (for example: { {"$regex" : "} } ). REGEX Searching in pymongo. Ask Question Asked 5 years, 11 months ago. Performing regex Queries with pymongo. 712. Check whether a string matches a regex in JS. 156.

To avoid the double compilation you can use the bson regex wrapper that comes with PyMongo:

>>> regx = bson.regex.Regex('^foo')
>>> db.users.find_one({"files": regx})

Regex just stores the string without trying to compile it, so find_one can then detect the argument as a 'Regex' type and form the appropriate Mongo query.

I feel this way is slightly more Pythonic than the other top answer, e.g.:

>>> db.collectionname.find({'files':{'$regex':'^File'}})

It's worth reading up on the bson Regex documentation if you plan to use regex queries because there are some caveats.

$regex, Provides regular expression capabilities for pattern matching strings in queries. Using pymongo with mongolab. GitHub Gist: instantly share code, notes, and snippets.

The solution of re doesn't use the index at all. You should use commands like:

db.collectionname.find({'files':{'$regex':'^File'}})

( I cannot comment below their replies, so I reply here )

$regexMatch (aggregation), Performs a regular expression (regex) pattern matching and returns: Prior to MongoDB 4.2, aggregation pipeline can only use the query operator $regex in the  For example, you might want to use a multiple-condition query request to find documents PyMongo. Sorting data may be helpful to you as well. Learn these techniques are more in this tutorial that shows you how to query MongoDB documents Python.

import re

def get_pattern_query(pattern,starting_with=False,ending_with=False,ignore_case=False):
    start = '^' if starting_with else '.*'
    end = '$' if ending_with else '.*'
    pattern = start + re.escape(pattern) + end
    return re.compile(pattern, re.IGNORECASE) if ignore_case else re.compile(pattern)

Escaping the pattern before compiling handles all characters.

How to Speed-Up MongoDB Regex Queries by a Factor of up-to 10, Why can't we just add an Index? Indexes are the first thing to consider when optimizing query performance with any database. The MongoDB  A regular expression is a “prefix expression” if it starts with a caret ( ^) or a left anchor ( \A ), followed by a string of simple symbols. For example, the regex /^abc.*/ will be optimized by matching only against the values from the index that start with abc.

Performing Regular Expression Searches in MongoDB, Performing Regular Expression Searches in MongoDB That way, the query will use make use of indexed values to match the regular  In most cases the query optimizer selects the optimal index for a specific operation; however, you can force MongoDB to use a specific index using the hint() method. Use hint() to support performance testing, or on some queries where you must select a field or field included in several indexes.

[100% Working Code], The string is stored in the regex so find_one can then detect the argument as a '​Regex' type and form the appropriate Mongo query. In Python  Introduction. When you query a database, you’re not always looking for an exact string match. You might be querying a collection of store inventory for all items that have “Cookies” as some part of their name, or perhaps you’re searching for a person’s last name that begins with “Sch”, though you’re not sure exactly how the name is spelled.

MongoDB Regular Expression ($option & $regex) with Examples , MongoDB regex tutorial- MongoDB regular expression for pattern matching with example, $regex operator, $option keyword, regex for array element, Query For doing exact pattern matching we use ^ and $ character. To select all documents in the collection, pass an empty document as the query filter parameter to the query bar. The query filter parameter determines the select criteria: To select all documents in the collection, pass an empty document as the query filter parameter to the find method.

Comments
  • Note also that regex's anchored at the start (ie: starting with ^) are able to use indexes in the db, and will run much faster in that case.
  • Regex's starting with ^ can only use an index in certain cases. When using re.IGNORECASE I believe mongo can't use an index to perform the query.
  • Is this usage documented somewhere? I can't find this in the official pymongo API doc.
  • Actually, what you have here is also the way it's done in javascript (and probably other languages too) if you use $regex. @Eric's answer is the python way that's a little different.
  • what's the difference? They're both using python pymongo correct? It is part of mongodb queries so I don't see the issue really.
  • Ignorecase is possible in regex of mongodb JScript also viz. db.collectionname.find({'files':{'$regex':'^File','$options':'i'}})
  • This answer looks better to my eyes. Why bother compiling a Python RE if you're just going to stringify it so that Mongo can compile it again? Mongo's $regex operator takes an $options argument.
  • Please use r'^File' instead of '^File' to avoid other problem
  • If you need to match agains an array using $in then $regex would not work for you. bson.regex.Regex will do the trick!