MongoDB $regex not working

This will be a quick tip of Mongodb:

I usually try my regular expressions in a Regular Expression “helper” I use gskinner and it’s really nice. My problem appears when here it perfectly works, and not in Mongodb shell.

My regexp:


This regular expression match with any word that starts with a decimal [0-9]

What was my mongodb query?

db.zips.find({ "city" : { $regex : "\d\w*" } })

Where was my mistake?


I thought that the regexp has to be inside “” and it has to be between / /

The solution

 db.zips.find({ "city" : { $regex : /\d\w*/ } }) 

It works perfectly!

This happened to me later in Nginx, I think mongodb and nginx use Perl PCRE.

Useful links

MongoDB $regex doc: $regex
Regular Expression Cheat Sheet
Regular Expression helper: gskinner

Social tagging:

Leave a Reply

Uso de cookies

Este sitio web utiliza cookies para que usted tenga la mejor experiencia de usuario. Si continúa navegando está dando su consentimiento para la aceptación de las mencionadas cookies y la aceptación de nuestra política de cookies, pinche el enlace para mayor información.plugin cookies