PageRenderTime 26ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/yqian1991/hellotest
Scala | 67 lines | 44 code | 11 blank | 12 comment | 3 complexity | 94a5bb06930441095120e44c9236d72d MD5 | raw file
  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. /*
  9. val mongoClient = MongoClient("localhost", 27017)
  10. insert(mongoClient, "test")
  11. //println(getCount(mongoClient, "test"))
  12. getCursor(mongoClient, "test")
  13. update(mongoClient, "test")
  14. remove(mongoClient, "test")
  15. drop(mongoClient, "test")
  16. //("s" \ "qianyu").text*/
  17. val algo = new Algo()
  18. println(algo.reverse(List(1, 4, 3, 0, 6)))
  19. }
  20. def insert(mongoClient: MongoClient, dbName:String): Unit = {
  21. val db = mongoClient(dbName)
  22. db.collectionNames
  23. val coll = db(dbName)
  24. val a = MongoDBObject("hello" -> "world")
  25. val b = MongoDBObject("language" -> "scala")
  26. coll.insert( a )
  27. coll.insert( b )
  28. }
  29. def getCount(mongoClient: MongoClient, dbName:String): Int = {
  30. return mongoClient(dbName)(dbName).count()
  31. }
  32. def getCursor(mongoClient: MongoClient, dbName:String): MongoCursor = {
  33. val allDocs = mongoClient(dbName)(dbName).find()
  34. println( allDocs )
  35. for(doc <- allDocs) println( doc )
  36. return allDocs
  37. }
  38. def update(mongoClient: MongoClient, dbName:String): Unit = {
  39. val coll = mongoClient(dbName)(dbName)
  40. val query = MongoDBObject("hello" -> "world")
  41. val update = MongoDBObject("platform" -> "iOS")
  42. val result = coll.update( query, update )
  43. println("Number updated: " + result.getN)
  44. for (c <- coll.find) println(c)
  45. }
  46. def remove(mongoClient: MongoClient, dbName:String): Unit = {
  47. val coll = mongoClient(dbName)(dbName)
  48. val query = MongoDBObject("language" -> "scala")
  49. val result = coll.remove( query )
  50. println("Number removed: " + result.getN)
  51. for (c <- coll.find) println(c)
  52. }
  53. def drop(mongoClient: MongoClient, dbName:String): Unit = {
  54. mongoClient(dbName)(dbName).drop()
  55. }
  56. }