Created
September 12, 2012 20:57
-
-
Save marcesher/3709872 to your computer and use it in GitHub Desktop.
mongodb examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dependencies { | |
runtime "org.mongodb:mongo-java-driver:2.9.1" | |
compile "org.mongodb:mongo-java-driver:2.9.1" | |
runtime "com.gmongo:gmongo:1.0" | |
} | |
plugins { | |
//runtime ":hibernate:$grailsVersion" | |
//runtime ":database-migration:1.1" | |
... | |
compile (":mongodb:1.0.0.GA"){ | |
excludes 'mongo-java-driver', 'gmongo' | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<ul class="bullets"> | |
<g:each in="${avgCityPopulation}" var="state"> | |
<li><b>${state._id}</b> -- ${state.avgCityPop} </li> | |
</g:each> | |
</ul> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def populationByState(){ | |
return zipcodes().aggregate( | |
[ $group : | |
[ _id : [ state : '$state', city : '$city' ], pop : [ $sum : '$pop' ] ] | |
], | |
[ $group : | |
[ _id : '$_id.state', avgCityPop : [ $avg : '$pop' ] ] | |
], | |
[ $sort: ['avgCityPop' : -1] ] | |
).results() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Result: [ | |
{ | |
"_id": "ND", | |
"avgCityPop": 1629.591836734694 | |
}, | |
{ | |
"_id": "SD", | |
"avgCityPop": 1826.7821522309712 | |
}, | |
{ | |
"_id": "VT", | |
"avgCityPop": 2315.8765432098767 | |
}, | |
.... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ZipCodeService { | |
def mongo | |
def getDB(){ | |
return mongo.getDB("MongoPlay") | |
} | |
def zipcodes(){ | |
return getDB().zipcodes | |
} | |
.... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment