Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

More breaking changes in Mongo.cfc! This includes argument reordering…

… and argument name changes to achieve a more consistent API. Arguments named "coll" have all been changed to "collectionName"; functions where mongoCollection was ordered before collectionName have now been reversed;
  • Loading branch information...
commit 35b4627fd2a893da8a2254697f2a11f5da0541c2 1 parent d0e5214
@marcesher marcesher authored
Showing with 45 additions and 47 deletions.
  1. +38 −42 core/Mongo.cfc
  2. +7 −5 test/MongoTest.cfc
View
80 core/Mongo.cfc
@@ -16,16 +16,21 @@
return this;
}
- function save(struct doc, string coll, mongoConfig=""){
- var collection = getMongoDBCollection(mongoConfig,coll);
+ function query(string coll, mongoConfig=""){
+ var db = getMongoDB(mongoConfig);
+ return new SearchBuilder(coll,db,mongoUtil);
+ }
+
+ function save(struct doc, string collectionName, mongoConfig=""){
+ var collection = getMongoDBCollection(collectionName, mongoConfig);
var bdbo = mongoUtil.newDBObjectFromStruct(doc);
collection.insert([bdbo]);
doc["_id"] = bdbo.get("_id");
return doc["_id"];
}
- function saveAll(array docs, string coll, mongoConfig=""){
- var collection = getMongoDBCollection(mongoConfig,coll);
+ function saveAll(array docs, string collectionName, mongoConfig=""){
+ var collection = getMongoDBCollection(collectionName, mongoConfig);
var i = 1;
var total = arrayLen(docs);
var allDocs = [];
@@ -36,34 +41,25 @@
return docs;
}
-
- function query(string coll, mongoConfig=""){
- var db = getMongoDB(mongoConfig);
- return new SearchBuilder(coll,db,mongoUtil);
- }
-
-
- function update(doc, coll, query={}, upsert=false, multi=false, mongoConfig=""){
- var collection = getMongoDBCollection(mongoConfig,coll);
+ function update(doc, collectionName, query={}, upsert=false, multi=false, mongoConfig=""){
+ var collection = getMongoDBCollection(collectionName, mongoConfig);
if(structIsEmpty(query)){
query = mongoUtil.newIDCriteriaObject(doc['_id'].toString());
} else{
- query = mongoUtil.newDBObjectFromStruct (query);
+ query = mongoUtil.newDBObjectFromStruct(query);
}
var dbo = mongoUtil.newDBObjectFromStruct(doc);
- collection.update( query, dbo, upsert, multi );
- } //end function
-
-
+ collection.update( query, dbo, upsert, multi );
+ }
- function remove(doc,coll,mongoConfig=""){
- var collection = getMongoDBCollection(mongoConfig,coll);
+ function remove(doc, collectionName, mongoConfig=""){
+ var collection = getMongoDBCollection(collectionName, mongoConfig);
var dbo = mongoUtil.newDBObjectFromStruct(doc);
collection.remove( dbo );
- } //end function
+ }
/**
* So important we need to provide top level access to it and make it as easy to use as possible.
@@ -72,11 +68,11 @@
http://www.mongodb.org/display/DOCS/findandmodify+Command
- This function assumes you are using this to *apply* additional changes to the "found" document. If you wish to overwrite, pass overwriteExisting=true. One bristles at the thought
+ This function assumes you are using this to *apply* additional changes to the "found" document. If you wish to overwrite, pass overwriteExisting=true. One bristles at the thought
*/
- function findAndModify(struct query, struct fields={}, struct sort={"_id"=1}, boolean remove=false, struct update, boolean returnNew=true, boolean upsert=false, boolean overwriteExisting=false, string coll){
- var collection = getMongoDBCollection (MongoConfig,coll);
+ function findAndModify(struct query, struct fields={}, struct sort={"_id"=1}, boolean remove=false, struct update, boolean returnNew=true, boolean upsert=false, boolean overwriteExisting=false, string collectionName, mongoConfig=""){
+ var collection = getMongoDBCollection(collectionName, mongoConfig);
//must apply $set, otherwise old struct is overwritten
if(not structKeyExists( update, "$set" ) and NOT overwriteExisting){
update = { "$set" = mongoUtil.newDBObjectFromStruct(update) };
@@ -108,8 +104,8 @@
]
*/
- public array function ensureIndex(array fields, coll, unique=false, mongoConfig=""){
- var collection = getMongoDBCollection(mongoConfig, coll);
+ public array function ensureIndex(array fields, collectionName, unique=false, mongoConfig=""){
+ var collection = getMongoDBCollection(collectionName, mongoConfig);
var pos = 1;
var doc = {};
var indexName = "";
@@ -129,40 +125,40 @@
var dbo = mongoUtil.newDBObjectFromStruct( doc );
collection.ensureIndex( dbo, "_#indexName#_", unique );
- return getIndexes(coll, mongoConfig);
+ return getIndexes(collectionName, mongoConfig);
}
- public array function getIndexes(coll, mongoConfig=""){
- var collection = getMongoDBCollection(mongoConfig, coll);
+ public array function getIndexes(collectionName, mongoConfig=""){
+ var collection = getMongoDBCollection(collectionName, mongoConfig);
var indexes = collection.getIndexInfo().toArray();
return indexes;
}
- public array function dropIndexes(coll, mongoConfig=""){
- getMongoDBCollection( mongoConfig, coll ).dropIndexes();
- return getIndexes( coll, mongoConfig );
+ public array function dropIndexes(collectionName, mongoConfig=""){
+ getMongoDBCollection( collectionName, mongoConfig ).dropIndexes();
+ return getIndexes( collectionName, mongoConfig );
}
//decide whether to use the one in the variables scope, the one being passed around as arguments, or create a new one
- function getMongoConfig(MongoConfig=""){
- if(isSimpleValue(arguments.MongoConfig)){
- MongoConfig = variables.mongoConfig;
+ function getMongoConfig(mongoConfig=""){
+ if(isSimpleValue(arguments.mongoConfig)){
+ mongoConfig = variables.mongoConfig;
}
- return MongoConfig;
+ return mongoConfig;
}
/* Provide access to the most common java objects */
- function getMongo(MongoConfig=""){
+ function getMongo(mongoConfig=""){
return variables.mongo;
}
- function getMongoDB(MongoConfig=""){
- var jMongo = getMongo(MongoConfig);
- return jMongo.getDb(getMongoConfig(MongoConfig).getDefaults().db_name);
+ function getMongoDB(mongoConfig=""){
+ var jMongo = getMongo(mongoConfig);
+ return jMongo.getDb(getMongoConfig(mongoConfig).getDefaults().db_name);
}
- function getMongoDBCollection(MongoConfig="",CollectionName=""){
- var jMongoDB = getMongoDB(MongoConfig);
+ function getMongoDBCollection(collectionName="", mongoConfig=""){
+ var jMongoDB = getMongoDB(mongoConfig);
return jMongoDB.getCollection(collectionName);
}
View
12 test/MongoTest.cfc
@@ -35,6 +35,8 @@ function tearDown(){
function deleteTest(){
+ mongo.ensureIndex(["somenumber"], col);
+ mongo.ensureIndex(["name"], col);
var doc = {
'name'='delete me',
'address' = {
@@ -179,7 +181,7 @@ function findAndModify_should_atomically_update_and_return_new(){
assertEquals(count, arrayLen(people));
var query = {inprocess=false};
var update = {inprocess=true, started=now(),owner=cgi.SERVER_NAME};
- var new = mongo.findAndModify(query=query, update=update, coll=atomicCol);
+ var new = mongo.findAndModify(query=query, update=update, collectionName=atomicCol);
flush();
debug(new);
@@ -199,12 +201,12 @@ function findAndModify_should_atomically_update_and_return_new(){
function testGetIndexes(){
- var result = mongo.dropIndexes(coll=col);
+ var result = mongo.dropIndexes(collectionName=col);
//guard
assertEquals( 1, arrayLen(result), "always an index on _id" );
- mongo.ensureIndex( coll=col, fields=["name"]);
- mongo.ensureIndex( coll=col, fields=[{"name"=1},{"address.state"=-1}]);
+ mongo.ensureIndex( collectionName=col, fields=["name"]);
+ mongo.ensureIndex( collectionName=col, fields=[{"name"=1},{"address.state"=-1}]);
result = mongo.getIndexes( col );
debug(result);
@@ -237,7 +239,7 @@ function getMongoDB_should_return_underlying_java_MongoDB(){
}
function getMongoDBCollection_should_return_underlying_java_DBCollection(){
- var jColl = mongo.getMongoDBCollection(mongoConfig,col);
+ var jColl = mongo.getMongoDBCollection(col, mongoConfig);
assertEquals("com.mongodb.DBApiLayer.mycollection",jColl.getClass().getCanonicalName());
}
Please sign in to comment.
Something went wrong with that request. Please try again.