Created
January 31, 2015 11:49
-
-
Save josdirksen/91d24ac1f632e2ae269f to your computer and use it in GitHub Desktop.
Database.scala
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
import reactivemongo.api._ | |
import reactivemongo.api.collections.default.BSONCollection | |
import reactivemongo.bson.BSONDocument | |
import scala.concurrent.ExecutionContext.Implicits.global | |
import scala.concurrent.Future | |
object Database { | |
val collection = connect() | |
def connect(): BSONCollection = { | |
val driver = new MongoDriver | |
val connection = driver.connection(List("localhost")) | |
val db = connection("akka") | |
db.collection("stocks") | |
} | |
def findAllTickers(): Future[List[BSONDocument]] = { | |
val query = BSONDocument() | |
val filter = BSONDocument("Company" -> 1, "Country" -> 1, "Ticker" -> 1) | |
// which results in a Future[List[BSONDocument]] | |
Database.collection | |
.find(query, filter) | |
.cursor[BSONDocument] | |
.collect[List]() | |
} | |
def findTicker(ticker: String) : Future[Option[BSONDocument]] = { | |
val query = BSONDocument("Ticker" -> ticker) | |
Database.collection | |
.find(query) | |
.one | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment