Asynchronous IO for MongoDB
val mongo = new MongoDriver
val connection = mymongo.connection(List("localhost"))
val db = myConnection.db("rmDemo")
val users = db.collection[BSONDocument]("users")
users.insert(BSONDocument("name" -> name, "age" -> 28))
def insert[T](document: T, ...)(...): Future[LastError]
users.insert(BSONDocument("name" -> name, "age" -> 28))
.map(lastError => println(lastError.ok))
users.remove(BSONDocument("name" -> "Tristan"))
.map(lastError => println(lastError.ok))
users.update(
BSONDocument("name" -> name),
BSONDocument("$inc" -> BSONDocument("age" -> 1))
)
.map(lastError => lastError.updated)
val cursor =
users.find(BSONDocument("name" -> "Tristan")).cursor[BSONDocument]
:Cursor[BSONDocument]
case class User(name: String, age: Int)
def read(bson: BSONDocument): User =
User(
bson.getAs[String]("name").get,
bson.getAs[Int]("age").get)
def write(user: User): BSONDocument =
BSONDocument(
"name" -> user.name,
"age" -> user.age)
case class User(name: String, age: Int)
implicit object User
extends BSONDocumentReader[User] with BSONDocumentWriter[User] {
def write(user: User): BSONDocument = BSONDocument(
"name" -> user.name,
"age" -> user.age
)
def read(bson: BSONDocument): User =
User(bson.getAs[String]("name").get,
bson.getAs[Int]("age").get)
}
val cursor = users.find(BSONDocument("name" -> name)).cursor[User]
users.insert(User("Tristan", 28))
.map(lastError => println(lastError.ok))
users.update(user, BSONDocument("$inc" -> BSONDocument("age" -> 1)))
.map(lastError => println(lastError.ok))
val users = db.collection[BSONCollection]("users")
val users = db.collection[JSONCollection]("users")
case class User(name: String, age: Int)
implicit val format = Json.format[User]
case class User(name: String, age: Int)
implicit val format = Json.format[User]
users.insert(User(name, 28))
.map(lastError => println(lastError.ok))
val cursor = users.find(Json.obj("age" -> 28)).cursor[User]
users.update(user, Json.obj("$inc" -> Json.obj("age" -> 1)))
.map(lastError => println(lastError.ok))
users.delete(user)
.map(lastError => println(lastError.ok))
One Json API