PageRenderTime 48ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/HelloTest/src/main/scala/com/yu/start/package.scala

https://gitlab.com/yqian1991/ScalaHub
Scala | 63 lines | 48 code | 11 blank | 4 comment | 3 complexity | 7aa4168ab8453e7b43fe437f5e691626 MD5 | raw file
Possible License(s): Apache-2.0
  1. package com.yu
  2. /**
  3. * Created by Yu on 15-04-24.
  4. */
  5. import com.mongodb.casbah.Imports._
  6. package object start {
  7. def main(args: Array[String]) {
  8. val mongoClient = MongoClient("localhost", 27017)
  9. insert(mongoClient, "test")
  10. //println(getCount(mongoClient, "test"))
  11. getCursor(mongoClient, "test")
  12. update(mongoClient, "test")
  13. remove(mongoClient, "test")
  14. drop(mongoClient, "test")
  15. }
  16. def insert(mongoClient: MongoClient, dbName:String): Unit = {
  17. val db = mongoClient(dbName)
  18. db.collectionNames
  19. val coll = db(dbName)
  20. val a = MongoDBObject("hello" -> "world")
  21. val b = MongoDBObject("language" -> "scala")
  22. coll.insert( a )
  23. coll.insert( b )
  24. }
  25. def getCount(mongoClient: MongoClient, dbName:String): Int = {
  26. return mongoClient(dbName)(dbName).count()
  27. }
  28. def getCursor(mongoClient: MongoClient, dbName:String): MongoCursor = {
  29. val allDocs = mongoClient(dbName)(dbName).find()
  30. println( allDocs )
  31. for(doc <- allDocs) println( doc )
  32. return allDocs
  33. }
  34. def update(mongoClient: MongoClient, dbName:String): Unit = {
  35. val coll = mongoClient(dbName)(dbName)
  36. val query = MongoDBObject("hello" -> "world")
  37. val update = MongoDBObject("platform" -> "iOS")
  38. val result = coll.update( query, update )
  39. println("Number updated: " + result.getN)
  40. for (c <- coll.find) println(c)
  41. }
  42. def remove(mongoClient: MongoClient, dbName:String): Unit = {
  43. val coll = mongoClient(dbName)(dbName)
  44. val query = MongoDBObject("language" -> "scala")
  45. val result = coll.remove( query )
  46. println("Number removed: " + result.getN)
  47. for (c <- coll.find) println(c)
  48. }
  49. def drop(mongoClient: MongoClient, dbName:String): Unit = {
  50. mongoClient(dbName)(dbName).drop()
  51. }
  52. }