Skip to content

Instantly share code, notes, and snippets.

@marcesher
Created September 12, 2012 20:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcesher/3709872 to your computer and use it in GitHub Desktop.
Save marcesher/3709872 to your computer and use it in GitHub Desktop.
mongodb examples
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'
}
}
<ul class="bullets">
<g:each in="${avgCityPopulation}" var="state">
<li><b>${state._id}</b> -- ${state.avgCityPop} </li>
</g:each>
</ul>
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()
}
Result: [
{
"_id": "ND",
"avgCityPop": 1629.591836734694
},
{
"_id": "SD",
"avgCityPop": 1826.7821522309712
},
{
"_id": "VT",
"avgCityPop": 2315.8765432098767
},
....
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