[SERVER-6500] Regex find with different counts Created: 18/Jul/12  Updated: 09/Jul/16  Resolved: 18/Jul/12

Status: Closed
Project: Core Server
Component/s: Shell
Affects Version/s: 2.1.2
Fix Version/s: None

Type: Question Priority: Major - P3
Reporter: erhan Assignee: Randolph Tan
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

ubuntu server 12.04 lts with 2GB ram ( virtual pc on MacOS X with Parallels Desktop)


Participants:

 Description   

Hello I was just testing how to perform SQL Like operations in MongoDB but I am quite confused about the results.
The usage of "/" char and "^" and also the results are strange.Is there something I am missing ?
Queries and results are below :

> db.user.find({Email:{$regex : "aslan"}}).count()
4174
> db.user.find({Email:{$regex : "^aslan"}}).count()
841
> db.user.find({Email:{$regex : "/^aslan/"}}).count()
0
> db.user.find({Email:{$regex : "/aslan/"}}).count()
0
> db.user.find({Email:{$regex : "aslan*"}}).count()
4410
> db.user.find({Email:{$regex : ".aslan."}}).count()
4174
> db.user.find({Email:{$regex : "/.aslan./"}}).count()
0

And I think there should be more explanation and example about regex on the page http://www.mongodb.org/display/DOCS/Advanced+Queries

Thnx



 Comments   
Comment by Randolph Tan [ 18/Jul/12 ]

Needs more explanation of the regex syntax. So we either document them or should at the very least direct them to the documentation of the pcre regex syntax (this is the library the server code currently uses to do regex matching).

Comment by Randolph Tan [ 18/Jul/12 ]

Hi,

You don't need the $regex if you want to use the // notation. Ex:

db.user.find({ Email: /aslan/ }); // equivalent to db.user.find({ Email: { $regex: "aslan" }})

^ is a marker used to indicate that strings will only count as a match if it the pattern is at the beginning of the string.

Ex. of strings that will match /^aslan/:

"aslander"
"aslang"

Ex. of strings that will not match /^aslan/:

"gaslander"
"www.aslan.com"

Generated at Thu Feb 08 03:11:52 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.