Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 27b92a295f
Fetching contributors…

Cannot retrieve contributors at this time

164 lines (120 sloc) 4.185 kb
<cfcomponent>
<cfscript>
/*---------------------------------------------------------------------
DSL for MongoDB searches:
mongo.expressionBuilder().
results = mongo.startsWith('name','foo'). //string
endsWith('title','bar'). //string
exists('field','value'). //string
regex('field','value'). //string
eq('field','value'). //numeric
lt('field','value'). //numeric
gt('field','value'). //numeric
gte('field','value'). //numeric
lte('field','value'). //numeric
in('field','value'). //array
nin('field','value'). //array
mod('field','value'). //numeric
size('field','value'). //numeric
search('title,author,date', limit, start);
search(keys=[keys_to_return],limit=num,start=num);
-------------------------------------------------------------------------------------*/
builder = createObject('java', 'com.mongodb.BasicDBObjectBuilder').start();
pattern = createObject('java', 'java.util.regex.Pattern');
function startsWith(element, val){
var regex = val & '.*';
builder.add( element, pattern.compile(regex) );
return this;
}
function endsWith(element, val){
var regex = '.*' & val;
builder.add( element, pattern.compile(regex) );
return this;
}
function exists(element, val){
var regex = '.*' & val & '.*';
builder.add( element, pattern.compile(regex) );
return this;
}
function regex(element, val){
var regex = val;
builder.add( element, pattern.compile(regex) );
return this;
}
function builder(){
return builder;
}
function start(){
builder.start();
return this;
}
function get(){
return builder.get();
}
//May need at least some exception handling
function where( js_expression ){
builder.add( '$where', js_expression );
return this;
}
function inArray(element, val){
builder.add( element, val );
return this;
}
//vals should be list or array
function $in(element,vals){
if(isArray(vals)) return addArrayCriteria(element,vals,'$in');
return addArrayCriteria(element, listToArray(vals),'$in');
}
function $nin(element,vals){
if(isArray(vals)) return addArrayCriteria(element,vals,'$nin');
return addArrayCriteria(element, listToArray(vals),'$nin');
}
function $eq(element,val){
builder.add( element, val );
return this;
}
function $ne(element,val){
return addNumericCriteria(element,val,'$ne');
}
function $lt(element,val){
return addNumericCriteria(element,val,'$lt');
}
function $lte(element,val){
return addNumericCriteria(element,val,'$lte');
}
function $gt(element,val){
return addNumericCriteria(element,val,'$gt');
}
function $gte(element,val){
return addNumericCriteria(element,val,'$gte');
}
</cfscript>
<!---
Note to self: Using cffunction here because of the ability/need to cast
arbitrary numeric data to java without using JavaCast. CFARGUMENT takes care
of that. CF9 might too, but most folks are still < CF9.
But, this also proved to be a very good refactor.
--->
<cffunction name="addNumericCriteria" hint="refactored $expressions for numerics">
<cfargument name="element" type="string" hint="The element in the document we're searching"/>
<cfargument name="val" type="numeric" hint="The comparative value of the element" />
<cfargument name="type" type="string" hint="$gt,$lt,etc. The operators - <><=>= ..." />
<cfscript>
var exp = {};
exp[type] = val;
builder.add( element, exp );
return this;
</cfscript>
</cffunction>
<cffunction name="addArrayCriteria" hint="refactored $expressions for numerics">
<cfargument name="element" type="string" hint="The array element in the document we're searching"/>
<cfargument name="val" type="array" hint="The value(s) of an element in the array" />
<cfargument name="type" type="string" hint="$in,$nin,etc." />
<cfscript>
var exp = {};
exp[type] = val;
builder.add( element, exp );
return this;
</cfscript>
</cffunction>
</cfcomponent>
Jump to Line
Something went wrong with that request. Please try again.