Last active
December 15, 2015 10:28
-
-
Save tmyymmt/5245450 to your computer and use it in GitHub Desktop.
copy from Janx Spirit http://janxspirit.blogspot.jp/2011/11/introduction-to-casbah-scala-mongodb.html
This file contains hidden or 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
// copy from Janx Spirit http://janxspirit.blogspot.jp/2011/11/introduction-to-casbah-scala-mongodb.html | |
//connect to a MongoDB server | |
val mongo = MongoConnection("anduin") | |
//create/connect to a collection | |
val coll = mongo("casbah_examples")("movies") | |
//drop collection | |
coll.dropCollection | |
//create some documents | |
val pf = MongoDBObject("title" -> "Pulp Fiction", | |
"director" -> "Quentin Tarantino", | |
"foo" -> "bar", | |
"actors" -> List("John Travolta", "Samuel L Jackson"), | |
"year" -> 1994) | |
val sw = MongoDBObject("title" -> "Star Wars", | |
"director" -> "George Lucas", | |
"cast" -> List("Harrison Ford", "Carrie Fisher"), | |
"year" -> 1977) | |
val fc = MongoDBObject("title" -> "Fight Club", | |
"director" -> "David Fincher", | |
"cast" -> List("Brad Pitt", "Edward Norton"), | |
"year" -> 1999) | |
//add some documents | |
coll += pf | |
coll += sw | |
coll += fc | |
val pfid = MongoDBObject("_id" -> pf.get("_id")) | |
//update a document | |
//increment a value (or set it if the value does not exist) | |
coll.update(pfid, $inc("likes" -> 1)) | |
//set a value | |
coll.update(pfid, $set("year" -> 1994)) | |
//remove a field | |
coll.update(pfid, $unset("foo")) | |
//add a value to an array - create it if need be | |
coll.update(pfid, $push("comebackKids" -> "John Travolta")) | |
//add a bunch of values to an array | |
coll.update(pfid, $pushAll("comebackKids" -> ("Bruce Willis", "Uma Thurman"))) | |
//add to a set (only adds values that were not there already) | |
coll.update(pfid, $addToSet("actors") $each("John Travolta","Groucho Marx","Harpo Marx","Alyssa Milano","Bruce Willis","Uma Thurman", "Jim Carrey")) | |
//remove last element of an array - use -1 to remove the first element | |
coll.update(pfid, $pop("actors" -> 1)) | |
//remove matching elements from a field | |
coll.update(pfid, $pull("actors" -> "Alyssa Milano")) | |
//remove more than one matching element from a field | |
coll.update(pfid, $pullAll("actors" -> ("Groucho Marx", "Harpo Marx"))) | |
//rename a field | |
coll.update(pfid, $rename("actors" -> "cast")) | |
//query documents | |
//find one by an exact field match | |
coll.findOne(MongoDBObject("title" -> "Star Wars")) | |
//find by regex | |
coll.find(MongoDBObject("title" -> ".*F".r)) | |
//leave out the ids | |
coll.find(MongoDBObject(),MongoDBObject("_id" -> 0)) | |
//include only certain fields without ids | |
coll.find(MongoDBObject(), MongoDBObject("title" -> 1, "cast" -> 1, "_id" -> 0)) | |
//find most recent | |
coll.find(MongoDBObject()).sort(MongoDBObject("year" -> -1)).limit(1) | |
//remove | |
coll.remove(MongoDBObject("title" -> "Fight Club")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment