Regular expressions in model.query

70 views
Skip to first unread message

Hugh Lomas

unread,
Aug 9, 2013, 11:57:01 AM8/9/13
to der...@googlegroups.com
Attempting the equivalent of 

var itemsQuery = model.query('items', { name: /a/i });

returns no results. Running db.items.find({ name: /a/i }) in mongo CLI returns results as expected.

Is this unsupported?

Dennis Torres

unread,
Aug 9, 2013, 12:41:13 PM8/9/13
to der...@googlegroups.com
I've found that embedding the regex directly like that doesn't work but rewriting it like this does:

model.query('items', {name: {$regex: 'a', $options: 'i'}});

Hugh Lomas

unread,
Aug 9, 2013, 12:49:05 PM8/9/13
to der...@googlegroups.com
Wonderful! I had tried 

model.query( 'items', { name: { $regex: /a/i } } ); 

without success but your syntax seems to be working great. Thank you!

Alexey Balekhov

unread,
Dec 2, 2015, 3:39:44 AM12/2/15
to Derby
Support for RegEx objects is necessary too. Because there is no way to use strings with $not operator: https://docs.mongodb.org/manual/reference/operator/query/not/#op._S_not . Is there any other hack for this?

пятница, 9 августа 2013 г., 20:49:05 UTC+4 пользователь Hugh Lomas написал:
Reply all
Reply to author
Forward
0 new messages