Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

got basics working

  • Loading branch information...
commit 051a51a50d6976d3af33ad37312eab1e5d87454d 1 parent d0f24d6
billy authored

Showing 3 changed files with 177 additions and 4 deletions. Show diff stats Hide diff stats

  1. +73 0 Mongo.cfc
  2. +27 3 MongoDB.cfc
  3. +77 1 MongoDBTest.cfc
73 Mongo.cfc
... ... @@ -0,0 +1,73 @@
  1 +<cfcomponent output="false">
  2 +<!--- Maybe should extend an Abstract Mongo? --->
  3 +
  4 +<cfscript>
  5 +//And this is configurable.
  6 +server_name = 'localhost';
  7 +server_port = 27017;
  8 +db_name = 'default_db';
  9 +collection_name = 'default_collection';
  10 +
  11 +//maybe this goes in super class
  12 +mongo = createObject('java', 'com.mongodb.Mongo').init( variables.server_name , variables.server_port );
  13 +db = mongo.getDb(db_name);
  14 +collection = db.getCollection(collection_name);
  15 +
  16 +
  17 +//------------------------------------------------------------------------//
  18 +
  19 +function put(){
  20 + var doc = createObject('java', 'com.mongodb.BasicDBObject').init();
  21 + if(arguments.size() eq 1){
  22 + doc.putAll(arguments[1]);
  23 + }
  24 + else {
  25 + doc.put(arguments[1],arguments[2]);
  26 + }
  27 +
  28 + return collection.insert(doc).get("_id");
  29 +}//end function
  30 +
  31 +
  32 +
  33 +function get(field,value){
  34 + var q = createObject('java', 'com.mongodb.BasicDBObject').init(field,value);
  35 + var cursor = collection.find(q);
  36 + return cursor.next();
  37 +} //end function
  38 +
  39 +
  40 +function count(){
  41 + return collection.getCount();
  42 +}
  43 +
  44 +function find(){
  45 +
  46 +} //end function
  47 +
  48 +
  49 +
  50 +function delete(field,value){
  51 + var q = createObject('java', 'com.mongodb.BasicDBObject').init(field,value);
  52 + return collection.remove(q); //id's of deleted items
  53 +} //end function
  54 +
  55 +
  56 +function update(o){
  57 + var q = createObject('java', 'com.mongodb.BasicDBObject').init(o);
  58 + return collection.save(q);
  59 +} //end function
  60 +
  61 +
  62 +
  63 +//not tested ...
  64 +function switchCollection(collection_name){
  65 + collection = db.getCollection(collection_name);
  66 +}
  67 +
  68 +</cfscript>
  69 +
  70 +<cffunction name="dump">
  71 + <cfdump var="#doc#">
  72 +</cffunction>
  73 +</cfcomponent>
30 MongoDB.cfc
@@ -4,6 +4,7 @@ variables.server_name = 'localhost';
4 4 variables.server_port = 27017;
5 5 variables.mongo = chr(0);
6 6 variables.db_name = "default_db";
  7 +variables.collection_name = "default_collection";
7 8
8 9 /********************************
9 10 mongo = createObject('java', 'com.mongodb.Mongo').init( "localhost" , 27017 );
@@ -18,16 +19,39 @@ function init(){
18 19 if( arguments.size() gte 2 ) setPort(arguments[2]);
19 20 if( arguments.size() gte 3 ) setDatabase(arguments[3]);
20 21 variables.mongo = createObject('java', 'com.mongodb.Mongo').init( getServer() , getPort() );
21   - variables.db_name = getDatabase();
  22 + variables.db_name = getDBName();
22 23 return this;
23 24 }
24 25
25 26
26   -function getDatabase(){
  27 +function getDb() {
  28 + if( arguments.size() ) {
  29 + return variables.mongo.getDB( arguments[1] );
  30 + }
  31 + else{
  32 + return variables.mongo.getDB( variables.db_name ); //default name
  33 + }
  34 +}
  35 +
  36 +
  37 +
  38 +
  39 +/*********************************************
  40 + acessor methods
  41 +***********************************************/
  42 +function setCollection(collection_name) {
  43 + variables.collection_name = arguments.collection_name;
  44 +}
  45 +
  46 +function getCollection() {
  47 + return variables.collection_name;
  48 +}
  49 +
  50 +function getDBName(){
27 51 return variables.db_name;
28 52 }
29 53
30   -function setDatabase(db_name){
  54 +function setDBName(db_name){
31 55 variables.db_name = arguments.db_name;
32 56 }
33 57
78 MongoDBTest.cfc
... ... @@ -1,9 +1,84 @@
1 1 <cfcomponent output="false" extends="mxunit.framework.TestCase">
2 2 <cfscript>
3 3
  4 + friend = {
  5 + name="f",
  6 + email = 'f@f.org',
  7 + address = {
  8 + street = '123 f st',
  9 + city = 'f',
  10 + state = 'f',
  11 + zip = '12345',
  12 + country = 'usa'
  13 + }
  14 + };
4 15
5   - function mongoShouldConnect() {
  16 + person = {
  17 + name="bill",
  18 + email = 'bill@mxunit.org',
  19 + address = {
  20 + street = '123 main st',
  21 + city = 'anytown',
  22 + state = 'here',
  23 + zip = '12345',
  24 + country = 'usa'
  25 + },
  26 + friend=friend
  27 + };
6 28
  29 +
  30 +function $endToEndSyntax(){
  31 + mongo = createObject('component','Mongo');
  32 + id = mongo.put(person); //name/value or struct
  33 + id2 = mongo.put('asdasd','xcvxcvxc');
  34 + the_guy = mongo.get('_id',id);
  35 + debug(id.toString());
  36 + debug(id2.toString());
  37 + the = mongo.get('_id',id2);
  38 + //debug(the);
  39 +
  40 + debug( mongo.count() );
  41 +
  42 + return;
  43 + mongo.delete('NAME','bill');
  44 + mongo.delete('asdasd','xcvxcvxc');
  45 + debug( mongo.count() );
  46 +}
  47 +
  48 +
  49 +
  50 +function $updatePerson(){
  51 + mongo = createObject('component','Mongo');
  52 + id = mongo.put(person); //name/value or struct
  53 + person.name = 'ed';
  54 + newperson = mongo.update(person);
  55 + debug(newperson);
  56 +
  57 +
  58 + mongo.delete('NAME','bill');
  59 + mongo.delete('NAME','ed');
  60 +
  61 +}
  62 +
  63 +
  64 +
  65 + person ={
  66 + name='bill',
  67 + email='bill@bill.com'
  68 + };
  69 +
  70 +
  71 + function basicDbTest() {
  72 + db = mongo.getDB("mydb");
  73 + assertEquals("com.mongodb.DBTCP", db.getClass().getName() );
  74 +
  75 + coll = db.getCollection("testCollection");
  76 +
  77 + doc = createObject('java', 'com.mongodb.BasicDBObject').init();
  78 + doc.putAll(person);
  79 +
  80 + coll.insert(doc);
  81 +
7 82 }
8 83
9 84
@@ -11,6 +86,7 @@
11 86 debug("these are defaults");
12 87 assert( 'localhost'== mongo.getServer() );
13 88 assert( '27017'== mongo.getPort() );
  89 + assert( 'default_db'== mongo.getDBName() );
14 90 }
15 91
16 92 function mongoShouldBeCool() {

0 comments on commit 051a51a

Please sign in to comment.
Something went wrong with that request. Please try again.