Skip to content
Browse files

Added $set to update; added more examples of update and $inc to getti…

…ngstarted.cfm
  • Loading branch information...
1 parent ce263f9 commit b0613131d36f26911133276510b78adc76525ecf @marcesher committed Nov 6, 2010
Showing with 79 additions and 5 deletions.
  1. +2 −3 .project
  2. +8 −2 core/Mongo.cfc
  3. +69 −0 examples/gettingstarted.cfm
View
5 .project
@@ -4,17 +4,16 @@
<comment></comment>
<projects>
</projects>
-<buildSpec>
+ <buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
-
<natures>
<nature>com.adobe.ide.coldfusion.projectNature</nature>
- <nature>org.cfeclipse.cfml.CFENature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
+ <nature>org.cfeclipse.cfml.CFENature</nature>
</natures>
</projectDescription>
View
10 core/Mongo.cfc
@@ -71,17 +71,23 @@
return docs;
}
- function update(doc, collectionName, query={}, upsert=false, multi=false, mongoConfig=""){
+ function update(doc, collectionName, query={}, upsert=false, multi=false, overwriteExisting=false, mongoConfig=""){
var collection = getMongoDBCollection(collectionName, mongoConfig);
if(structIsEmpty(query)){
query = mongoUtil.newIDCriteriaObject(doc['_id'].toString());
+ var dbo = mongoUtil.toMongo(doc);
} else{
query = mongoUtil.toMongo(query);
- }
+ var keys = structkeyList(doc);
+ if(not find("$",keys) and NOT overwriteExisting){
+ doc = { "$set" = mongoUtil.toMongo(doc) };
+ }
+ }
var dbo = mongoUtil.toMongo(doc);
+
collection.update( query, dbo, upsert, multi );
}
View
69 examples/gettingstarted.cfm
@@ -125,6 +125,75 @@ h2{
writeOutput("<h2>Find by ID</h2>");
writeDump(var=byID, label="Find by ID: #url.personID#", expand="false");
+ //here's how to update. You'll generally do two kinds of updating:
+ // 1) updating a single pre-fetched document... this is the most common. It's a find/modify/resave
+ // 2) updating one or more documents based on criteria. You almost always need to use a $set in this situation!!!
+
+ //updating a single pre-fetched document
+ person = mongo.query( collection ).search(limit="1").asArray()[1];
+ person.FAVORITECIGAR = "H. Upmann Cubano";
+ person.MODTS = now();
+ arrayAppend( person.KIDS, {NAME = "Pauly", AGE = 0} );
+ mongo.update( person, collection );
+
+ writeOutput("<h2>Updated Person</h2>");
+ writeDump( var=person, label="updated person", expand="false");
+
+ //updating a single document. by default it'll wrap the "doc" arg in "$set" as a convenience
+ person = {NAME = "Ima PHP dev", AGE=12};
+ mongo.save( person, collection );
+
+ mongo.update( doc={NAME = "Ima CF Dev", HAPPY = true}, query= {NAME = "Ima PHP dev"}, collectionName = collection );
+ afterUpdate = mongo.findById( person["_id"], collection );
+
+ writeOutput("<h2>Updated person by criteria</h2>");
+ writeDump(var = person, label="Original", expand=false);
+ writeDump(var = afterUpdate, label = "After update", expand=false);
+
+ //updating a single document based on criteria and overwriting instead of updating
+ person = {NAME = "Ima PHP dev", AGE=12};
+ mongo.save( person, collection );
+
+ mongo.update( doc={NAME = "Ima CF Dev", HAPPY = true}, query= {NAME = "Ima PHP dev"}, overwriteExisting = true, collectionName = collection );
+ afterUpdate = mongo.findById( person["_id"], collection );
+
+ writeOutput("<h2>Updated person by criteria. Notice it OVERWROTE the entire document</h2>");
+ writeDump(var = person, label="Original", expand=false);
+ writeDump(var = afterUpdate, label = "After update without using $set", expand=false);
+
+
+ //updating multiple documents
+ mongo.saveAll(
+ [{NAME = "EmoHipster", AGE=16},
+ {NAME = "EmoHipster", AGE=15},
+ {NAME = "EmoHipster", AGE=18}],
+ collection
+ );
+
+ mongo.update( doc = {NAME = "Oldster", AGE=76, REALIZED="tempus fugit"}, query = {NAME = "EmoHipster"}, multi=true, collectionName = collection );
+
+ oldsters = mongo.query( collection ).$eq("NAME","Oldster").search().asArray();
+
+ writeOutput("<h2>Updating multiple documents</h2>");
+ writeDump( var=oldsters, label="Even EmoHipsters get old some day", expand="false");
+
+ //perform an $inc update
+ cast = [{NAME = "Wesley", LIFELEFT=50, TORTUREMACHINE=true},
+ {NAME = "Spaniard", LIFELEFT=42, TORTUREMACHINE=false},
+ {NAME = "Giant", LIFELEFT=6, TORTUREMACHINE=false},
+ {NAME = "Poor Forest Walker", LIFELEFT=60, TORTUREMACHINE=true}];
+
+ mongo.saveAll( cast, collection );
+
+ suckLifeOut = {"$inc" = {LIFELEFT = -1}};
+ victims = {TORTUREMACHINE = true};
+ mongo.update( doc = suckLifeOut, query = victims, multi = true, collectionName = collection );
+
+ rugenVictims = mongo.query( collection ).$eq("TORTUREMACHINE", true).search().asArray();
+
+ writeOutput("<h2>Atomically incrementing with $inc</h2>");
+ writeDump( var = cast, label="Before the movie started", expand=false);
+ writeDump( var = rugenVictims, label="Instead of sucking water, I'm sucking life", expand=false);
//findAndModify: Great for Queuing!
//insert docs into a work queue; find the first 'pending' one and modify it to 'running'

0 comments on commit b061313

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