My Adventures in Coding

May 19, 2011

MongoDB – Negative Regex Query in Mongo shell

Filed under: MongoDB — Brian @ 8:26 am
Tags: , , ,

MongoDB’s interactive shell, supports the use of Regular Expressions as one of their Advanced Query options. The other day it came up that I needed to do a query for all documents where the text of a field did NOT start with a given pattern. It took a little bit of trial and error to get the regex right in the mongo shell, so here is what worked for us.

So let’s say for example we have a MongoDB database with a collection called “cars”. The primary key for each document is “make:model”, for example “Ford:Fusion”.

To find all cars made by “Ford”, you would just query for all documents where the id starts with the text “Ford”:

db.cars.find({"_id":/^Ford/})

But, if you want to query for all cars that are NOT made by “Ford”, you can do this with a negative regex such as:

db.cars.find({"_id":/^((?!Ford).)/})

Obviously, this type of regex is not efficient, but if you just need to do a quick query from the Mongo shell, it can come in handy.

I hope that saves you some time!

4 Comments »

  1. well, I wouldn’t put the make in the _id field to begin with, but sticking with your example, there’s a simpler solution:

    db.cars.find({ “_id” : { ‘$not’ : /^Ford/ } })

    Comment by secesh — September 22, 2012 @ 9:24 am | Reply

  2. It;s nor working in my case
    Refer to the query below

    db.users.find({languages:/^((?!Hadoop).)/})
    “_id” : ObjectId(“507572a07d416b0aebf86bfc”), “name” : “Namit Taneja”, “languages” : [ “Hadoop”, “MongoDB” ] }
    “_id” : ObjectId(“5075705e7d416b0aebf86bfa”), “name” : “Devender Singh”, “languages” : [ “SSIS”, “Business Objects” ] }

    Accdto the query the Hadoop document should not have been there in the output.

    Comment by Namit Taneja — November 2, 2012 @ 2:31 am | Reply

  3. Namit, the query returned that document not because it falsely matched on Hadoop, but because it correctly matched on MongoDB. You would need to construct a more specific query to state that ALL values in the array must match (ie., or rather, not match, as that is what here constitutes a match…).

    Comment by bathos — September 15, 2014 @ 11:25 am | Reply

  4. […] MongoDB – Negative Regex Query in Mongo shell | My … – May 19, 2011 · Namit, the query returned that document not because it falsely matched on Hadoop, but because it correctly matched on MongoDB. You would need to …… […]

    Pingback by Fix Mongo Error Need To Login Windows XP, Vista, 7, 8 [Solved] — November 25, 2014 @ 10:58 pm | Reply


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: