Skip to content

Instantly share code, notes, and snippets.

@Jedt3D
Created October 15, 2012 18:20
Show Gist options
  • Save Jedt3D/3894133 to your computer and use it in GitHub Desktop.
Save Jedt3D/3894133 to your computer and use it in GitHub Desktop.
MongoDB + OpenBlueDragon for GridFS
// --------------------------------------------------------------------------------- function testAddRemoveFindOne(){ MongoRegister( name="mongo", server="127.0.0.1", db="openbd" ); var id = MongoGridfsSave( "mongo", "filebucket", ExpandPath("MongoDatabase.cfc"), "mongodatabase.cfc", "text/plain", "4f6e7d23c243305d41cd748c", {custom1:1, custom2:"age"} ); var f = MongoGridfsFindOne(datasource="mongo", bucket="filebucket", _id=id); assertEquals( f._id, "4f6e7d23c243305d41cd748c" ); assertEquals( f.filename, "mongodatabase.cfc" ); assertEquals( f.contentType, "text/plain" ); assertTrue( f.length > 0 ); assertEquals( f.metadata.custom1, 1 ); assertEquals( f.metadata.custom2, "age" ); // Delete it from the database MongoGridfsRemove( datasource="mongo", bucket="filebucket", _id=id ); f = MongoGridfsFindOne(datasource="mongo", bucket="filebucket", _id=id); assertTrue( StructCount(f) == 0 ); } // --------------------------------------------------------------------------------- function testAddFindRemove(){ MongoRegister( name="mongo", server="127.0.0.1", db="openbd" ); var id = MongoGridfsSave( "mongo", "filebucket", ExpandPath("MongoDatabase.cfc"), "mongodatabase.cfc", "text/plain", "4f6e7d23c243305d41cd748c", {custom1:1, custom2:"age"} ); var arry = MongoGridfsFind(datasource="mongo", bucket="filebucket", query={filename:"mongodatabase.cfc"} ); assertTrue( ArrayLen(arry) == 1 ); // Delete it from the database MongoGridfsRemove( datasource="mongo", bucket="filebucket", _id=id ); f = MongoGridfsFindOne(datasource="mongo", bucket="filebucket", _id=id); assertTrue( StructCount(f) == 0 ); } // --------------------------------------------------------------------------------- function testAddGetRemove(){ MongoRegister( name="mongo", server="127.0.0.1", db="openbd" ); var id = MongoGridfsSave( "mongo", "filebucket", ExpandPath("MongoDatabase.cfc"), "mongodatabase.cfc", "text/plain", "4f6e7d23c243305d41cd748c", {custom1:1, custom2:"age"} ); var res = MongoGridfsGet(datasource="mongo", bucket="filebucket", filepath=ExpandPath('deleteme.cfc'), _id=id ); assertTrue( res ); FileDelete( ExpandPath('deleteme.cfc') ); // Delete it from the database MongoGridfsRemove( datasource="mongo", bucket="filebucket", _id=id ); f = MongoGridfsFindOne(datasource="mongo", bucket="filebucket", _id=id); assertTrue( StructCount(f) == 0 ); }
// ---------------------------------------------------------------------------------
function testAddRemoveFindOne() {
MongoRegister(name = "mongo", server = "127.0.0.1", db = "openbd");
var id = MongoGridfsSave("mongo", "filebucket", ExpandPath("MongoDatabase.cfc"), "mongodatabase.cfc", "text/plain", "4f6e7d23c243305d41cd748c", {custom1:1, custom2:"age"});
var f = MongoGridfsFindOne(datasource = "mongo", bucket = "filebucket", _id = id);
assertEquals(f._id, "4f6e7d23c243305d41cd748c");
assertEquals(f.filename, "mongodatabase.cfc");
assertEquals(f.contentType, "text/plain");
assertTrue(f.length > 0);
assertEquals(f.metadata.custom1, 1);
assertEquals(f.metadata.custom2, "age");
// Delete it from the database
MongoGridfsRemove(datasource = "mongo", bucket = "filebucket", _id = id);
f = MongoGridfsFindOne(datasource = "mongo", bucket = "filebucket", _id = id);
assertTrue(StructCount(f) == 0);
}
// ---------------------------------------------------------------------------------
function testAddFindRemove() {
MongoRegister(name = "mongo", server = "127.0.0.1", db = "openbd");
var id = MongoGridfsSave("mongo", "filebucket", ExpandPath("MongoDatabase.cfc"), "mongodatabase.cfc", "text/plain", "4f6e7d23c243305d41cd748c", {custom1:1, custom2:"age"});
var arry = MongoGridfsFind(datasource = "mongo", bucket = "filebucket", query = {filename:"mongodatabase.cfc"});
assertTrue(ArrayLen(arry) == 1);
// Delete it from the database
MongoGridfsRemove(datasource = "mongo", bucket = "filebucket", _id = id);
f = MongoGridfsFindOne(datasource = "mongo", bucket = "filebucket", _id = id);
assertTrue(StructCount(f) == 0);
}
// ---------------------------------------------------------------------------------
function testAddGetRemove() {
MongoRegister(name = "mongo", server = "127.0.0.1", db = "openbd");
var id = MongoGridfsSave("mongo", "filebucket", ExpandPath("MongoDatabase.cfc"), "mongodatabase.cfc", "text/plain", "4f6e7d23c243305d41cd748c", {custom1:1, custom2:"age"});
var res = MongoGridfsGet(datasource = "mongo", bucket = "filebucket", filepath = ExpandPath('deleteme.cfc'), _id = id);
assertTrue(res);
FileDelete(ExpandPath('deleteme.cfc'));
// Delete it from the database
MongoGridfsRemove(datasource = "mongo", bucket = "filebucket", _id = id);
f = MongoGridfsFindOne(datasource = "mongo", bucket = "filebucket", _id = id);
assertTrue(StructCount(f) == 0);
}
@Jedt3D
Copy link
Author

Jedt3D commented Oct 15, 2012

It's not JavaScript but to make syntax highlight work a bit more. This ColdFusion Script is rename to .js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment