12. “MongoDB is a document-oriented
database management system
designed for performance, horizontal
scalability, high availability, and
advanced queryability”
source: http://docs.mongodb.org/manual/about/
20. MySQL MongoDB
database database
table collection
index index
row BSON document
column BSON field
join embedding and linking
primary key _id field
group by aggregation
source: mongodb.org
22. Embedding vs Linking
“Embedding is the nesting of objects and arrays inside a
BSON document”
“Links are references between documents”
source: http://www.mongodb.org/display/DOCS/Schema+Design
23. MongoDB on MacOS X
$ wget http://downloads.mongodb.org/osx/mongodb-osx-x86_64-2.0.6.tgz
$ tar -zxvf mongodb-osx-x86_64-2.0.6.tgz
$ sudo mkdir -p /data/db
$ sudo chown `id -u` /data/db
44. Casbah DBObject
import org.mongodb.casbah.Imports._
val mongoConn = MongoConnection(hostname, port)
val mongoDB = mongoConn(“test_db”)
val collection = mongoDB(“test_data”)
val newObj = MongoDBObject(“a” -> “apple”, “b” -> “banana”)
newObj += “c” -> “chocolate”
val a = newObject.getAs[String](“a”)
// a is an Option[String]
http://api.mongodb.org/scala/casbah/current/tutorial.html
45. Casbah MongoDBList
// MongoDBList is a Mongo-friendly Scala List
import org.mongodb.casbah.Imports._
val builder = MongoDBList.newBuilder
builder += "foo"
builder += "bar"
builder += "x"
builder += "y"
val newLst = builder.result
/* newLst: com.mongodb.BasicDBList = [ "foo" ,
"bar" , "x" , "y"] */
http://api.mongodb.org/scala/casbah/current/tutorial.html
46. Querying with Casbah
val mongoColl = MongoConnection()("test_db")("users")
val user1 = MongoDBObject("user" -> "barack",
"email" -> "barack@whitehouse.gov")
val user2 = MongoDBObject("user" -> "someOtherUser")
mongoColl += user1
mongoColl += user2
mongoColl.find()
// com.mongodb.casbah.MongoCursor =
// MongoCursor{Iterator[DBObject] with 2 objects.}
for { x <- mongoColl} yield x
/* Iterable[com.mongodb.DBObject] = List(
{ "_id" : { "$oid" : "4c3e2bec521142c87cc10faa"} ,
"user" : "obama" ,
"email" : "barack@whitehouse.gov"},
{ "_id" : { "$oid" : "4c3e2bec521142c87dc10fbb"} ,
"user" : "someOtherUser"}
) */
http://api.mongodb.org/scala/casbah/current/tutorial.html
47. Fluid querying with Casbah DSL
val q = "email" $exists true
// q: (String, com.mongodb.DBObject) =
// (email,{ "$exists" : true})
val users = for (x <- mongoColl.find(q))
yield x
assert(users.size == 1)
http://api.mongodb.org/scala/casbah/current/tutorial.html
54. “Fongo is an in-memory java implementation of mongo.”
“[...] primary use is for lightweight unit testing where you
don't want to spin up a mongo process”