How to use $regexp?

4,657 views
Skip to first unread message

Jehu

unread,
May 25, 2011, 11:33:29 AM5/25/11
to Mongoose Node.JS ORM
Hi,

i wonder, how to use mongodb $regex expression:

Contact.find({'firstname': { $regex: ['/jehu/
i'] }}).sort('lastname','ASC', 'company', 'ASC').run(function (err,
docs) {
...
});

ends up in this error:
Error: Can't use $regex with String.

Field 'firstname' is from type 'String', what else should it be?

I got the idea using regex when I saw that post:
http://groups.google.com/group/mongoose-orm/msg/30b0dd507bbe4a9c

Aaron Heckmann

unread,
May 25, 2011, 5:15:27 PM5/25/11
to mongoo...@googlegroups.com
You can pass a RegExp directly:

  Contact.find({firstname: /jehu/i })
--
Aaron


Jehu

unread,
May 27, 2011, 3:30:02 AM5/27/11
to Mongoose Node.JS ORM
Great! Thank you.

Philmod

unread,
Jun 17, 2011, 3:37:35 AM6/17/11
to Mongoose Node.JS ORM
Where does come this error from?

I use '$regex' as well, it works on my computer, but not on my
friend's computer!??

Thank you,
Philippe

brig

unread,
Apr 10, 2012, 1:47:11 PM4/10/12
to mongoo...@googlegroups.com
How do you pass a parameter value?

var fname = 'john';
contact.find({'firstname': /fname/i});

This doesn't work.

Phill Rosen

unread,
Apr 10, 2012, 2:56:12 PM4/10/12
to mongoo...@googlegroups.com
Try

var patt=new RegExp(pattern,modifiers);
contact.find({'firstname': patt});

> --
> http://mongoosejs.com
> http://github.com/learnboost/mongoose
> You received this message because you are subscribed to the Google
> Groups "Mongoose Node.JS ORM" group.
> To post to this group, send email to mongoo...@googlegroups.com
> To unsubscribe from this group, send email to
> mongoose-orm...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/mongoose-orm?hl=en

Steve Portock

unread,
Jan 3, 2014, 2:54:22 PM1/3/14
to mongoo...@googlegroups.com, phill...@gmail.com
So what does work for this?

I tried all of these none work, but if I run the regex at mongolab in their shell, the regex works.

How do you do a regex with mongoose, if I can't do this, I have to go some other route, I really would like to use this and not have to drop back to the native driver if possible.

Thanks in advance.

Steve

Joe Wagner

unread,
Jan 3, 2014, 3:03:02 PM1/3/14
to mongoo...@googlegroups.com, marco....@gmail.com
@Steve Portock
If you can't get any of the examples in this thread to work, I would guess that something else is wrong in your code.
What failure specifically are you experiencing?

Steve Portock

unread,
Jan 3, 2014, 4:04:44 PM1/3/14
to mongoo...@googlegroups.com, marco....@gmail.com
I did not do this correctly,

I figured out what I did wrong.

app.get('/home/like/:name', function(req,res){
var r = new RegExp(req.params.name,'i');
Users.find({name: {$regex:r}},function(err,docs){
res.json({users:docs});
});
});

That works pretty well, I am finding myself getting addicted to coding with this now lol.

Steve
Reply all
Reply to author
Forward
0 new messages