Permalink
Browse files

added more usage examples

  • Loading branch information...
1 parent 88b3a5c commit ce263f93dce3dbe28a7ae028e8fdec65ffcec923 @marcesher marcesher committed Nov 6, 2010
Showing with 158 additions and 20 deletions.
  1. +4 −14 examples/benchmark.cfm
  2. +12 −6 examples/gettingstarted.cfm
  3. +28 −0 examples/initMongo.cfm
  4. +37 −0 examples/loadPeople.cfm
  5. +70 −0 examples/peopleList.cfm
  6. +7 −0 examples/personDetail.cfm
View
@@ -1,22 +1,12 @@
-<!--- pass 'false' in the URL to use the mongo jars in your cfusion lib directory --->
-<cfparam name="url.useJavaLoader" default="true">
-
+<!--- create the mongo objects --->
+<cfset variables.dbName = "cfmongodb_benchmarks">
+<cfinclude template="initMongo.cfm">
+<cfset mongoUtil = mongo.getMongoUtil()>
<cfscript>
serverName = server.coldfusion.productname & "_" & server.coldfusion.productversion;
- if( url.useJavaLoader ){
- javaloaderFactory = createObject('component','cfmongodb.core.JavaloaderFactory').init();
- mongoConfig = createObject('component','cfmongodb.core.MongoConfig').init(dbName="cfmongodb_benchmarks", mongoFactory=javaloaderFactory);
- }
- else
- {
- mongoConfig = createObject('component','cfmongodb.core.MongoConfig').init(dbName="cfmongodb_benchmarks");
- }
- mongo = createObject('component','cfmongodb.core.Mongo').init(mongoConfig);
- mongoUtil = mongo.getMongoUtil();
-
//we'll create/use a 'people' collection
collection = "datadump";
metricColl = "metrics";
@@ -42,7 +42,7 @@ h2{
doc =
{
NAME = "Marc",
- WIFE = "Heather",
+ SPOUSE = "Heather",
KIDS = [
{NAME="Alexis", AGE=7, HAIR="blonde", DESCRIPTION="crazy" },
{NAME="Sidney", AGE=2, HAIR="dirty blonde", DESCRIPTION="ornery" }
@@ -66,14 +66,13 @@ h2{
*/
-
//here's how to insert multiple documents
coolPeople = [];
for( i = 1; i LTE 5; i++ ){
DOC =
{
- NAME = "Cool Dude #i#",
- WIFE = "Smokin hot wife #i#",
+ NAME = "Cool Person #i#",
+ SPOUSE = "Cool Spouse #i#",
KIDS = [
{NAME="kid #i#", age=randRange(1,80), HAIR="strawberry", DESCRIPTION="fun" },
{NAME="kid #i+1#", age=randRange(1,80), HAIR="raven", DESCRIPTION="joyful" }
@@ -96,9 +95,9 @@ h2{
specialized = mongo.query( collection ).$eq("BIKE", "Specialized").search();
showResult( specialized, "Specialized riders" );
- //find the 2nd and 3rd Specialized bike riders, sorted by "ts" descending
+ //find the 3rd and 4th Specialized bike riders, sorted by "ts" descending
specialized = mongo.query( collection ).$eq("BIKE", "Specialized").search( skip=2, limit=2, sort="TS=-1" );
- showResult( specialized, "Specialized riders, skipping to 2, limiting to 2, sorting by ts desc (skip is 0-based!)" );
+ showResult( specialized, "Specialized riders, skipping to 3, limiting to 2, sorting by ts desc (skip is 0-based!)" );
//find riders with counter between 1 and 3, sorted by "ts" descending
specialized = mongo.query( collection )
@@ -147,6 +146,13 @@ h2{
writeOutput("<h2>findAndModify()</h2>");
writeDump(var=nowScheduled, label="findAndModify", expand="false");
+ writeOutput("<h2>Indexes</h2>");
+ //here's how to add indexes onto collections for faster querying
+ mongo.ensureIndex( ["NAME"], collection );
+ mongo.ensureIndex( ["BIKE"], collection );
+ mongo.ensureIndex( ["KIDS.AGE"], collection );
+ writeDump(var=mongo.getIndexes(collection), label="Indexes", expand="false");
+
//show how you get timestamp creation on all documents, for free, when using the default ObjectID
View
@@ -0,0 +1,28 @@
+
+<!--- pass 'false' in the URL to use the mongo jars in your cfusion lib directory --->
+<cfparam name="url.useJavaLoader" default="true">
+
+
+<cfparam name="variables.dbName" default="mongorocks">
+
+<cfscript>
+ if( url.useJavaLoader ){
+ //the fastest way to try out cfmongodb is using Mark Mandel's javaloader, which we ship with cfmongodb. Thanks Mark!
+ //http://javaloader.riaforge.org
+
+ //use the cfmongodb javaloader factory
+ javaloaderFactory = createObject('component','cfmongodb.core.JavaloaderFactory').init();
+
+ //create a default MongoConfig instance; in your real apps, you'll create an object that extends MongoConfig and put your environment specific config in there
+ //here we initialize it with a db named 'mongorocks'
+ mongoConfig = createObject('component','cfmongodb.core.MongoConfig').init(dbName=variables.dbName, mongoFactory=javaloaderFactory);
+ }
+ else
+ {
+ mongoConfig = createObject('component','cfmongodb.core.MongoConfig').init(dbName=variables.dbName);
+ }
+
+ //initialize the core cfmongodb Mongo object
+ mongo = createObject('component','cfmongodb.core.Mongo').init(mongoConfig);
+
+</cfscript>
View
@@ -0,0 +1,37 @@
+<cfset dbName = "mongorocks">
+<cfset collection = "people">
+
+<cfif not structKeyExists(variables, "mongo")>
+ <cfinclude template="initMongo.cfm">
+</cfif>
+
+<cfscript>
+
+counters = mongo.distinct( "COUNTER",collection );
+arraySort( counters, "numeric", "desc" );
+
+if( NOT arrayIsEmpty(counters) ){
+ nextNum = counters[1] + 1;
+} else {
+ nextNum = 1;
+}
+
+coolPeople = [];
+for( i = nextNum; i LTE nextNum + 50; i++ ){
+ DOC =
+ {
+ NAME = "Cool Person #i#",
+ SPOUSE = "Cool Spouse #i#",
+ KIDS = [
+ {NAME="kid #i#", age=randRange(1,80), HAIR="strawberry", DESCRIPTION="fun" },
+ {NAME="kid #i+1#", age=randRange(1,80), HAIR="raven", DESCRIPTION="joyful" }
+ ],
+ BIKE = "Specialized",
+ TS = now(),
+ COUNTER = i
+ };
+ arrayAppend( coolPeople, doc );
+}
+
+mongo.saveAll( coolPeople, collection );
+</cfscript>
View
@@ -0,0 +1,70 @@
+
+<cfinclude template="initMongo.cfm">
+
+<!--- <cfinclude template="loadPeople.cfm"> --->
+
+<cfparam name="url.limit" default="25">
+<cfparam name="url.skip" default="0">
+
+<cfset results = mongo.query("people")
+ .search(skip=url.skip, limit = url.limit)>
+
+<cfset people = results.asArray()>
+
+<cfoutput>
+<p align="center">Showing #url.skip+1# through #results.size()# of #results.totalCount()# People</p>
+
+
+<cfsavecontent variable="navlinks">
+ <cfif results.totalCount() GT results.size()>
+ <p align="center" class="pagination">
+ <span>
+ <a href="peopleList.cfm?skip=#url.skip-url.limit#">Previous</a>
+ </span>
+
+ <span>
+ <a href="peopleList.cfm?skip=#url.skip+url.limit#">Next</a>
+ </span>
+ </p>
+ </cfif>
+</cfsavecontent>
+
+#navlinks#
+
+<!--- see all this structKeyExists() stuff below... this is why you want to keep your documents structured the same... so you don't need this! --->
+<table border="1" align="center">
+ <thead>
+ <tr>
+ <th>N</th>
+ <th>Name</th>
+ <th>Spouse</th>
+ <th>Kids</th>
+ </tr>
+ </thead>
+
+
+ <tbody>
+ <cfloop array="#people#" index="person">
+ <tr>
+ <td> #IIF( structKeyExists(person, "counter") , DE("#person.counter#"), DE('&nbsp;') )# </td>
+ <td> <a href="personDetail.cfm?id=#person['_id'].toString()#">#person.name#</a> </td>
+ <td> #IIF( structKeyExists(person, "spouse") , DE("#person.spouse#"), DE('&nbsp;') )# </td>
+ <td>
+ <cfif structKeyExists(person, "kids")>
+ <cfloop array="#person.kids#" index="kid">
+ #kid.name# |
+ </cfloop>
+ <cfelse>
+ None
+ </cfif>
+ </td>
+ </tr>
+ </cfloop>
+ </tbody>
+</table>
+
+#navlinks#
+
+</cfoutput>
+<cfset mongo.close()>
+
@@ -0,0 +1,7 @@
+<cfinclude template="initMongo.cfm">
+
+<cfset person = mongo.findById(url.id, "people")>
+<h1>Person <cfoutput>#url.id#</cfoutput></h1>
+<cfdump var="#person#">
+
+<cfset mongo.close()>

0 comments on commit ce263f9

Please sign in to comment.