Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

adding expression builder for search DSL

  • Loading branch information...
commit 027af5559af71a7aecf8e46492703e2a803df92a 1 parent 8b68bf4
unknown authored
View
91 part2/ExpressionBuilder.cfc
@@ -0,0 +1,91 @@
+<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 $gt(element, val){
+ //need to address all numeric values!
+ var exp = {};
+ exp[element] = javacast('long', val);
+ builder.add( '$gt', exp );
+ return this;
+}
+
+
+
+function builder(){
+ return builder;
+}
+
+function start(){
+ return builder.start();
+}
+
+function get(){
+ return builder.get();
+}
+
+
+</cfscript>
+</cfcomponent>
View
71 part2/ExpressionBuilderTest.cfc
@@ -0,0 +1,71 @@
+<cfcomponent extends="mxunit.framework.TestCase">
+<cfscript>
+ mongo = createObject('component','MongoDB');
+ coll = mongo.getCollection('blog');
+ key_exp = {AUTHOR=1,TITLE=1,TS=1};
+ keys = createObject('java', 'com.mongodb.BasicDBObject').init(key_exp);
+
+
+function testRegEx(){
+ temp = builder.regex( 'TITLE', '.*No.600$');
+ q = createObject('java', 'com.mongodb.BasicDBObject').init(builder.get());
+ items = coll.find( q, keys );
+ ia = items.toArray();
+ debug(items.count());
+ debug(items.toArray().toString());
+ debug(items.toArray());
+ assert( ia.size() );
+ assertEquals( ia[1]["TITLE"], "Blog Title No.600" );
+ assertEquals( ia[1]["AUTHOR"], "bill_600" );
+}
+
+
+function testGT(){
+ temp = builder.$gt( 'TS', 1256148921000);
+ q = createObject('java', 'com.mongodb.BasicDBObject').init(builder.get());
+ items = coll.find( q, keys );
+ ia = items.toArray();
+ debug(items.count());
+ debug(items.toArray().toString());
+ fail( 'last test of the day. must.go.home.' );
+}
+
+
+function testExists(){
+ temp = builder.exists( 'TITLE', 'Title No.600');
+ q = createObject('java', 'com.mongodb.BasicDBObject').init(builder.get());
+ items = coll.find( q, keys );
+ ia = items.toArray();
+ debug(items.count());
+ debug(items.toArray().toString());
+ debug(items.toArray());
+ assertEquals( ia[1]["TITLE"], "Blog Title No.600" );
+ assertEquals( ia[1]["AUTHOR"], "bill_600" );
+}
+
+
+function testStartsWith(){
+ temp = builder.startsWith( 'AUTHOR', 'bill_60');
+ q = createObject('java', 'com.mongodb.BasicDBObject').init(builder.get());
+ items = coll.find( q, keys );
+ debug(items.count());
+ debug(items.toArray().toString());
+ assertEquals( 11, items.count() );
+}
+
+function testEndsWith(){
+ temp = builder.endsWith( 'TITLE', 'Title No.609');
+ q = createObject('java', 'com.mongodb.BasicDBObject').init(builder.get());
+ items = coll.find( q, keys );
+ debug(items.count());
+ debug(items.toArray().toString());
+ assertEquals( 1, items.count() );
+}
+
+function setUp(){
+ builder = createObject('component','ExpressionBuilder');
+}
+
+
+</cfscript>
+</cfcomponent>
View
3  part2/MongoDB.cfc
@@ -101,6 +101,9 @@ function getCollection(collection_name){
return collection;
}
+
+
+
</cfscript>
</cfcomponent>
View
82 part2/MongoDBTest.cfc
@@ -27,6 +27,7 @@
exits('field','value').
mod('field','value').
size('field','value').
+ regex('field','value').
search('title,author,date');
@@ -35,11 +36,84 @@
search(keys=[keys_to_return],limit=num,start=num);
+ Use BasicDBObjectBuilder.start().add( "name" , "eliot" ).add( "number" , 17 ).get()
+
Note: Look at aggregation
- */
+ -------------------------------------------------------------------------------------*/
+
+
+
+ function $exploreStringSearchExpression(){
+ coll = mongo.getCollection('blog');
+
+ key_exp = {TITLE=1,TS=1,AUTHOR=1};
+ keys = createObject('java', 'com.mongodb.BasicDBObject').init(key_exp);
+
+ debug( 'bill' > 'z' );
+
+ exp = createObject('java', 'com.mongodb.BasicDBObjectBuilder').start();
+ //exp.add( '$ne', javacast('long',1256148920969) ); //bill_792
+ exp.add( '$gt', 'bill_792' );
+
+ debug( exp.get() );
+
+ q = createObject('java', 'com.mongodb.BasicDBObject').init("AUTHOR", exp.get() );
+ items = coll.find( q, keys );
+ debug(items.count());
+ debug(items.toArray().toString());
+
+ }
+
+
+ function $buildSearchExpressionWithBuilder(){
+ coll = mongo.getCollection('blog');
+
+ // BasicDBObjectBuilder.start().add( "name" , "eliot" ).add( "number" , 17 ).get()
+
+ key_exp = {TITLE=1,TS=1};
+ keys = createObject('java', 'com.mongodb.BasicDBObject').init(key_exp);
+
+ exp = createObject('java', 'com.mongodb.BasicDBObjectBuilder').start();
+ exp.add( '$gte', javacast('long',1256148921000 ) );
+ exp.add( '$lte', javacast('long',1256148921000 ) );
+
+ debug( exp.get() );
+
+ q = createObject('java', 'com.mongodb.BasicDBObject').init("TS", exp.get() );
+ items = coll.find( q, keys );
+ debug(items.count());
+ debug(items.toArray().toString());
+
+ }
+
+
+ function $findByExpression(){
+ coll = mongo.getCollection('blog');
+
+ key_exp = {TITLE=1,TS=1};
+ keys = createObject('java', 'com.mongodb.BasicDBObject').init(key_exp);
+
+ // { a : { $gt: 3 } }
+ //exp['$where'] = 'this.TS > 1256133363469';
+ //exp = { $gt=1256133363469 };
+ //exp = {};
+ exp['$gte']= javacast('long',1256148921000 );
+ exp['$lte']= javacast('long',1256148921000 );
+ debug( exp );
+
+ //q = createObject('java', 'com.mongodb.BasicDBObject').init("TS", javacast('long',1256148921000) );
+ q = createObject('java', 'com.mongodb.BasicDBObject').init("TS", exp );
+ items = coll.find( q, keys );
+
+ debug(items.count());
+ debug(items.toArray().toString());
+ assert(items.count() > 0);
+
+ }
+
function $findByRegEx(){
coll = mongo.getCollection('blog');
@@ -49,8 +123,8 @@
//import java.util.regex.Pattern;
//import java.util.regex.Matcher;
p = createObject('java', 'java.util.regex.Pattern').compile('bill_6[0-2].*');
- exp = { AUTHOR=p, };
- key_exp = {AUTHR=1,TITLE=1};
+ exp = { AUTHOR=p };
+ key_exp = {AUTHOR=1,TITLE=1,TS=1};
keys = createObject('java', 'com.mongodb.BasicDBObject').init(key_exp);
q = createObject('java', 'com.mongodb.BasicDBObject').init(exp);
items = coll.find( q, keys );
@@ -85,7 +159,7 @@
for(i; i < 1000;i++){
entry.title = 'Blog Title No.' & i;
entry.author = 'bill_' & i;
- entry.ts = getTickCount();
+ entry.ts = javacast('long',getTickCount());
entry.body = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. In eu justo lectus. Morbi aliquet consectetur consequat. Mauris mauris nulla, condimentum in aliquet nec, suscipit in lacus. Donec vel ante ut metus imperdiet interdum. Curabitur non sapien at felis egestas bibendum in eu urna. In rutrum ligula erat. Integer tristique viverra consequat. Curabitur tristique velit vel nunc aliquet congue eleifend lacus cursus. Aenean a lorem a arcu tincidunt tempus. Nulla faucibus diam in sem consequat tincidunt. Aenean quis nunc vitae leo luctus porta. Etiam justo enim, imperdiet vel commodo sed, placerat nec dolor. Nunc placerat sapien id ligula varius eget tempus est eleifend.
Vivamus ipsum justo, interdum ut blandit feugiat, tempus id erat. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Nulla dignissim eros quis velit vestibulum ullamcorper. Donec mattis venenatis augue sed semper. Donec egestas dapibus mauris cursus elementum. Phasellus egestas porta quam vitae sodales. Vestibulum ut tortor vitae enim mollis placerat. Sed tortor orci, venenatis sit amet auctor in, condimentum vitae enim. In vel nulla velit. Proin ultrices vehicula nibh a interdum. Integer rhoncus luctus est sit amet placerat. In commodo, mauris id consectetur pulvinar, massa nibh condimentum nunc, non fermentum lacus turpis vitae nisl. Curabitur at dui eu leo laoreet luctus in eu diam. Aliquam ante velit, venenatis sit amet sollicitudin non, adipiscing in orci. Curabitur tristique bibendum volutpat. Donec blandit tempor vestibulum. Sed lorem justo, fringilla vel hendrerit sit amet, pellentesque lobortis turpis. Integer sed turpis quis dolor suscipit mollis in a nibh. Aenean blandit condimentum erat, eget consequat erat cursus eu. Vestibulum in massa tortor.
View
54 query/AdvancedQueryTest.cfc
@@ -0,0 +1,54 @@
+<cfcomponent output="false" extends="mxunit.framework.TestCase">
+<cfscript>
+
+ function queryTest(){
+ mongo = createObject('component','cfmongodb.MongoDB');
+ mongo.switchCollection('mongoq');
+
+ objects = mongo.query('title:blog');
+
+
+
+
+ }
+
+
+ function setUp(){
+ }
+
+ function tearDown(){
+
+ }
+
+
+</cfscript>
+
+
+
+<!---
+<cffunction name="genBlogPosts" access="private">
+ <cfscript>
+ var mongo = createObject('component','cfmongodb.MongoDB');
+ var mongo.switchCollection('mongoq');
+ var tags = ['food','technology','sports','religon'];
+ var blog = '';
+ var i = 1;
+
+ for(i=1; i <= 100; i++){
+ idx = randRange(1,arrayLen(tags));
+ blog = {};
+ blog.title = 'Blog Number #i#';
+ blog.author = 'bill#i#';
+ blog.text = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Duis tellus. Donec ante dolor, iaculis nec, gravida ac, cursus in, eros. Mauris vestibulum, felis et egestas ullamcorper, purus nibh vehicula sem, eu egestas ante nisl non justo. Fusce tincidunt, lorem nec dapibus consectetuer, leo orci mollis ipsum, eget suscipit eros purus in ante. Wait, what am I saying? Never mind.';
+ blog.tags = tags[idx];
+ blog.pub_date = now();
+ mongo.put(blog);
+ }
+
+ </cfscript>
+
+</cffunction>
+--->
+
+
+</cfcomponent>
Please sign in to comment.
Something went wrong with that request. Please try again.