Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alaz/375157 to your computer and use it in GitHub Desktop.
Save alaz/375157 to your computer and use it in GitHub Desktop.
// import for MongoDB Scala Driver
import com.osinka.mongodb._ // <- this replaces Preamble
import com.osinka.mongodb.shape._
import com.mongodb.{Mongo,DBObject}
// Scala complains you are using "case class" here and I understand it. "class" is more
// appropriate as you have no arguments
class User extends MongoObject {
var id: Int = _
var name: String = _
var age: Option[Int] = None
override def toString: String = "User " + id + " " + name + " " + age
}
object User extends MongoObjectShape[User] {
lazy val id = Field.scalar("id", _.id, (x: User, v: Int) => x.id = v)
lazy val name = Field.scalar("name", _.name, (x: User, v: String) => x.name = v)
lazy val age = Field.optional("age", _.age, (x: User, v: Option[Int]) => x.age = v)
override lazy val * = id :: name :: age :: Nil
override def factory(dbo: DBObject) = Some(new User)
}
// I've made it "object" inheriting Application just to be able to test
object ExampleMongoDriverUsage extends Application {
// for a little variation
val r = new scala.util.Random
// possible last names
val lastNames = List("Washington", "Roosevelt", "Johnson", "Kennedy", "Taylor", "Lincoln", "Harrison")
val Host = "localhost"
val Port = 27017
val Database = "kolly"
val mongo = new Mongo(Host, Port).getDB(Database)
def tryThis() {
println("Hello Mongo! Let's play with scala...")
// create a test collection (like a table)
val testDbColl = mongo.getCollection("test")
val testColl = testDbColl.asScala
// insert Map directly into collection
testColl << Map("key" -> 10)
// insert User objects into a User collection
println("Adding users...")
val userDbColl = mongo.getCollection("user")
val userColl = userDbColl of User
for (i <- 1 to 50) {
val useri = new User
useri.name = "Iam "+lastNames(r.nextInt(lastNames.length - 1))
useri.id = i
useri.age = Some(10 + (r.nextInt(70))) // because User.age is an Option[Int]
userColl << useri
}
println
// get the users
println("Retrieve all Users...")
userColl foreach { u => println(u.name) }
// to get only one "first" user and print its name:
userColl.headOption foreach { u => println(u.name) }
// to iterate over users using for-comprehension and return names only:
val names = for {u <- userColl} yield u.name
println
// query for first two users
println("First two users...")
for {u <- User take 2 in userColl} println(u)
println
// query with a 'where'
println("Some users older than 50...")
val agedUsers = User where {User.age is_> 50} take 10 in userColl
val nAged = agedUsers.size // an estimate of users satisfying the query (not greater than 10)
for {u <- agedUsers} println(u)
println
// query with a join term
println("Users younger than 50 in the Kennedy family...")
for {u <- User where { (User.name like "Kennedy".r) and (User.age is_< 50) } in userColl} println(u)
println
// drop some users
println("Dropping users older than 65, who should be fishing")
println(" and users younger than 20, who should be in school...")
for { u <- User where { (User.age is_> 65) and (User.age is_< 20) } in userColl} {
userColl -= u
}
println
println("Remaining users in age order...")
for { u <- User sortBy User.age.ascending in userColl} println(u)
}
tryThis
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment