Where filter, case insensitive search

1,877 views
Skip to first unread message

Nicole Chung

unread,
Aug 25, 2014, 12:10:37 PM8/25/14
to loopb...@googlegroups.com
Is there a way to use the "where" filter so that the search is case-insensitive?

I can make my query lowercase, but really what I want to do is be able to search for say, "USA", "usa", "Usa"... so all versions will show up in my search results.

Post.find(
{ where: {title: { like: ''.*'+query.toLowerCase()+'.*''} } }, 
  function (err, posts) { ... }
);

Raymond Feng

unread,
Aug 25, 2014, 12:20:15 PM8/25/14
to Nicole Chung, loopb...@googlegroups.com
What databases do you want to have this support? We’re considering to add an regexp operator.

Thanks,

---
Raymond Feng
Co-Founder and Architect @ StrongLoop, Inc.

StrongLoop makes it easy to develop APIs in Node, plus get DevOps capabilities like monitoring, debugging and clustering.

--
You received this message because you are subscribed to the Google Groups "LoopbackJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to loopbackjs+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Nicole Chung

unread,
Aug 25, 2014, 12:33:33 PM8/25/14
to loopb...@googlegroups.com, nicole...@gmail.com
MongoDB for now. Thanks!

Raymond Feng

unread,
Aug 25, 2014, 12:41:37 PM8/25/14
to Nicole Chung, loopb...@googlegroups.com
On the node side, the like operator can take a RegExp object for mongodb. Please give a try.

Thanks,

---
Raymond Feng
Co-Founder and Architect @ StrongLoop, Inc.

StrongLoop makes it easy to develop APIs in Node, plus get DevOps capabilities like monitoring, debugging and clustering.

Nicole Chung

unread,
Aug 25, 2014, 3:04:03 PM8/25/14
to loopb...@googlegroups.com, nicole...@gmail.com
This works, thanks.

var pattern = new RegExp('.*'+query+'.*', "i"); /* case-insensitive RegExp search */ 

Post.find(

{ where: {title: { like: pattern} } }, 

  function (err, posts) { ... }

);

Partap Davis

unread,
Aug 25, 2014, 6:59:01 PM8/25/14
to loopb...@googlegroups.com, nicole...@gmail.com
Is there a way to do this from REST?

I've got something like this (using angular):

filter.include = ['author']
if (scope.search) {
  filter.where = {
   'or': [
     {title:          {like : scope.search}}
     {'author.name':  {like : scope.search}}
     {descr:          {like : scope.search}}
   ]  
  }
}
Video.find({filter:filter})

The 'author.name' bit doesn't work either, but that's a different issue...

It would be nice if I could pass literal regex in... or maybe use an operator (regex or just re)
{title: {like: re: {'/' + scope.search + '/i'}}

But that would probably have to be built into the core of loopback...
Is there a way I could implement something like this by intercepting the REST call with a hook?  I'm still a little fuzzy on what you can do with hooks...
Maybe just rewrite any "like" query to use a case-insensitive regex?

Raymond Feng

unread,
Aug 25, 2014, 7:08:54 PM8/25/14
to Partap Davis, loopb...@googlegroups.com, nicole...@gmail.com
You can plug in a beforeRemote hook to transform the filter object.

Thanks,

---
Raymond Feng
Co-Founder and Architect @ StrongLoop, Inc.

StrongLoop makes it easy to develop APIs in Node, plus get DevOps capabilities like monitoring, debugging and clustering.

Partap Davis

unread,
Aug 25, 2014, 7:46:55 PM8/25/14
to loopb...@googlegroups.com, chee...@gmail.com, nicole...@gmail.com


On Monday, August 25, 2014 5:08:54 PM UTC-6, Raymond Feng wrote:
You can plug in a beforeRemote hook to transform the filter object.

Thanks!

I've got a hook set up now...I'm trying to figure out where to find the parsed filter object that I need to transform.
The closest I've come so far is ctx.args, which has parsed the URL params into the object:
{
  filter: "json string here..."
}

But since I need to convert the "like" clauses from strings to regexp objects, the json string doesn't help me.

There doesn't seem to be much documentation for the ctx object...

Raymond Feng

unread,
Aug 25, 2014, 7:53:32 PM8/25/14
to Partap Davis, loopb...@googlegroups.com, Nicole Chung, Ritchie Martori
Is the filter still a json string? I thought it’s already a json object. Ritchie, can you confirm?

Thanks,

---
Raymond Feng
Co-Founder and Architect @ StrongLoop, Inc.

StrongLoop makes it easy to develop APIs in Node, plus get DevOps capabilities like monitoring, debugging and clustering.

Partap Davis

unread,
Aug 25, 2014, 9:39:55 PM8/25/14
to loopb...@googlegroups.com, chee...@gmail.com, nicole...@gmail.com, rit...@strongloop.com
Yeah, it's a string.  
I tried parsing it and sticking it back into ctx.args, but no luck...it needs to be a string.
Probably a simple fix to skip the parse step later down the line, but I don't know where to look...
Message has been deleted
Message has been deleted

Leo B

unread,
Mar 14, 2015, 3:11:36 PM3/14/15
to loopb...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages