How to write this $or statement

42 views
Skip to first unread message

Alex Sante

unread,
Feb 28, 2014, 11:29:04 AM2/28/14
to cfmo...@googlegroups.com
Hey Folks,

Sorry if this is a dumb question. I'm trying to update the query args below to read: "If firstname contains what was provided in rc.searchArgs.searchText .. OR lastname containts... or surveyForm.title contains.. .etc"

if(structKeyExists(rc.searchArgs, "searchText") && len(rc.searchArgs.searchText)){
searchArgs['$or'] = [ {'firstname': rc.searchArgs.searchText}, 
 {'lastname': rc.searchArgs.searchText},
 {'surveyForm.title': rc.searchArgs.searchText} ]
}

Right now, it's doing an exact match on these keys.

Any help is appreciated.  Thanks!

Marc Esher

unread,
Mar 4, 2014, 6:59:47 AM3/4/14
to cfmo...@googlegroups.com
Alex,

Did you ever figure this out?

Mongo does an exact match by default, and so you need to use Regex to simulate a LIKE query. In cfmongodb, we do it like this:

pattern = createObject('java', 'java.util.regex.Pattern');
var regex = '.*' & val & '.*';
pattern.compile(regex, ignoreCase ? pattern.CASE_INSENSITIVE : 0)

In context, that's at/near:

So if you were doing a case-insensitive $OR, your case might look something like:

pattern = createObject('java', 'java.util.regex.Pattern');
var search = pattern.compile('.*' & rc.searchArgs.searchText & '.*');

searchArgs['$or'] = [ {'firstname': search}, 
  {'lastname': search},
  {'surveyForm.title': search} ]

Best,

Marc


--
You received this message because you are subscribed to the Google Groups "CFMongoDB" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cfmongodb+...@googlegroups.com.
To post to this group, send email to cfmo...@googlegroups.com.
Visit this group at http://groups.google.com/group/cfmongodb.
For more options, visit https://groups.google.com/groups/opt_out.

Alex Sante

unread,
Mar 6, 2014, 11:37:40 AM3/6/14
to cfmo...@googlegroups.com
Thanks for the reply Marc.

I tried using the regex functionality when I was first trying this all out.  I'll give it another shot this evening, and report my findings. 

Thanks again.

Marc Esher

unread,
Mar 15, 2014, 3:20:35 PM3/15/14
to cfmo...@googlegroups.com
Hey Alex, 

How'd you make out with this?

Marc

Alex Sante

unread,
Apr 1, 2014, 10:27:34 PM4/1/14
to cfmo...@googlegroups.com
Hey Marc,

Sorry it's taken me soooooo long to reply.  The regex approach worked after some tinkering. Here's my complete (working) solution:

param name="rc.searchArgs" value="#structNew()#";
var searchArgs = {'completed': false, 'deleted': false};
var collection = [];
var pattern = createObject('java', 'java.util.regex.Pattern');

// Merge search arguments if they exist
if(structKeyExists(rc,"searchArgs")){
if(structKeyExists(rc.searchArgs, "searchText") && len(rc.searchArgs.searchText)){
// (?i) --> Disables case sensitivity
var search = pattern.compile('(?i).*' & rc.searchArgs.searchText & '.*');
searchArgs['$or'] = [{'lastname': search},{'firstname': search},{'surveyForm.title': search}];
collection.addAll(model('Survey').find(criteria=searchArgs).asArray());
}else{
collection.addAll(model('Survey').find(criteria=searchArgs).asArray());
}
}

Thanks for your help!
Reply all
Reply to author
Forward
0 new messages