reactivemongo

How to convert JSON String to a BSONDocument

你说的曾经没有我的故事 提交于 2019-12-19 04:51:07
问题 I have the following function that uses the reactivemongo driver and actually does a good job writing to the database. def writeDocument() = { val document = BSONDocument( "firstName" -> "Stephane", "lastName" -> "Godbillon", "age" -> 29) val future = collection.insert(document) future.onComplete { case Failure(e) => throw e case Success(result) => { println("successfully inserted document with result = " + result) } } } But the limitation of that function is that the JSON is hardcoded into a

Console scala app doesn't stop when using reactive mongo driver

若如初见. 提交于 2019-12-14 03:53:53
问题 I'm playing with Mongo database through the Reactive Mongo driver import org.slf4j.LoggerFactory import reactivemongo.api.MongoDriver import reactivemongo.api.collections.default.BSONCollection import reactivemongo.bson.BSONDocument import scala.concurrent.Future import scala.concurrent.duration._ import scala.concurrent.ExecutionContext.Implicits.global object Main { val log = LoggerFactory.getLogger("Main") def main(args: Array[String]): Unit = { log.info("Start") val conn = new MongoDriver

Play 2.2 EssentialAction With Futures

£可爱£侵袭症+ 提交于 2019-12-13 07:07:14
问题 I'm trying to implement an authentication mechanism similar to this example: def HasToken(action: String => EssentialAction): EssentialAction = EssentialAction { requestHeader => val maybeToken = requestHeader.headers.get("X-SECRET-TOKEN") maybeToken map { token => action(token)(requestHeader) // apply requestHeader to EssentialAction produces the Iteratee[Array[Byte], SimpleResult] } getOrElse { Done(Unauthorized("401 No Security Token\n")) // 'Done' means the Iteratee has completed its

using akka streams to go over mongo collection

ぃ、小莉子 提交于 2019-12-13 03:58:22
问题 I have a collection of people in mongo, and I want to go over each person in the collection as a stream, and for each person call a method that is performing api call, changing the model, and inserting to a new collection in mongo. It looks like this: def processPeople()(implicit m: Materializer): Future[Unit] = { val peopleSource: Source[Person, Future[State]] = collection.find(json()).cursor[Person]().documentSource() peopleSource.runWith(Sink.seq[Person]).map(people => { people.foreach

Upsert many records using ReactiveMongo and Scala

三世轮回 提交于 2019-12-12 18:42:16
问题 I am writing a DAO Actor for MongoDB that uses ReactiveMongo. I want to implement some very simple CRUD operations, among which the ability to upsert many records in one shot. Since I have a reactive application (built on Akka), it's important for me to have idempotent actions, so I need the operation to be an upsert, not an insert. So far I have the following (ugly) code to do so: case class UpsertResult[T](nUpd: Int, nIns: Int, failed: List[T]) def upsertMany[T](l: List[T], collection:

Get an error message while tring to rewrite ReactiveMongo + BSON to JSON in Play Framework

戏子无情 提交于 2019-12-12 02:53:53
问题 I tried to use Json library to replace Bson library. This is the original code which works. case class City(name: String, population: Int) object City { implicit val reader = Macros.reader[City] } @Singleton class CityController @Inject()(val reactiveMongoApi: ReactiveMongoApi)(implicit exec: ExecutionContext) extends Controller with MongoController with ReactiveMongoComponents { def findByMinPopulation(minPop: Int) = Action.async { import citiesBSON.BatchCommands.AggregationFramework.Match

ReactiveMongo: How to deserialize a list into an Option

被刻印的时光 ゝ 提交于 2019-12-12 02:53:33
问题 Here below is a case class that represents an user, and its companion object provides a BSONDocumentWriter and BSONDocumentReader to serialize/deserialize to/from BSON: case class User( id: Option[BSONObjectID], name: String, addresses: Option[List[BSONObjectID]] ) object User { implicit object UserWriter extends BSONDocumentWriter[User] { def write(user: User) = BSONDocument( "_id" -> user.id.getOrElse(BSONObjectID.generate), "name" -> user.name, "addresses" -> user.addresses ) } implicit

MongoDB update a document when exists already with ReactiveMongo

女生的网名这么多〃 提交于 2019-12-11 11:27:56
问题 I'm writing a Scala web application that use MongoDB as database and ReactiveMongo as driver. I've a collection named recommendation.correlation in which I saved the correlation between a product and a category. A document has the following form: { "_id" : ObjectId("544f76ea4b7f7e3f6e2db224"), "category" : "c1", "attribute" : "c3:p1", "value" : { "average" : 0, "weight" : 3 } } Now I'm writing a method as following: def calculateCorrelation: Future[Boolean] = { def calculate(category: String,

Play2 Framework/Scala/Specs2 - Do different FakeApplications share Singleton objects?

北战南征 提交于 2019-12-10 09:49:14
问题 I'm quite new to Play2 and Scala. I'm writing a simple application that uses ReactiveMongo plugin. I wrote a simple object to use as DAO object UserDAO { def db: reactivemongo.api.DB = ReactiveMongoPlugin.db def collection: JSONCollection = db.collection[JSONCollection]("users") collection.indexesManager.ensure(Index(List("name" -> IndexType.Ascending), unique = true)) def insert(User): Future[LastError] = { collection.insert(unit) } def findOne(query: JsObject): Future[Option[User]] = {

ReactiveMongo: How to convert BSON returned by FindAndModify to JSON

喜欢而已 提交于 2019-12-09 19:35:03
问题 Here below is the code for updating a document with Mongo's FindAndModify : val selector = BSONDocument("id" -> "1234") val modifier = BSONDocument("$set" -> BSONDocument("email" -> "new@domain.com")) ReactiveMongoPlugin.db.command(FindAndModify( collection.name, selector, Update(modifier, false), false, None )).transform( success => success.map { s => // doesn't work... Json.fromJson[Seq[JsValue]](toJson(s)).map(for (item <- _) yield item).get }.getOrElse(List[JsValue]()), failure => failure