mongodb - Bulk insert / Insert many with Play Framework, ReactiveMongo -
i building app using play framework 2.5.0 , reactivemongo , spending lot of time, stuck, on easy in web languages.
that thing inserting many documents @ once. so, must use reactivemongo function bulkinsert
.
i found this google group had simple example, 2013 , signature changed
from
def bulkinsert[t](enumerator: enumerator[t])
to
def bulkinsert(documents: stream[p.document], ordered: boolean, writeconcern: writeconcern)(implicit ec: executioncontext): future[multibulkwriteresult]
so here tried take example , find way convert enumerator stream (did not find way so) :
val schemasdocs: seq[jsobject] = { jsonschemas.fields.map { case (field, value) => json.obj(field -> value) } } val enumerator = enumerator.enumerate(schemasdocs) val schemasstream = source.frompublisher(streams.enumeratortopublisher(enumerator)) // attempt turn enumerator stream val schemasinsert = { getcollection("schemas").flatmap( _.bulkinsert(schemasstream, true) ) }
now find myself diving in akka, reactivemongo , play api try , create stream of jsobjects seq of jsobjects..
then tried different approach: example reactivemongo's website
val bulkdocs = schemasdocs.map(implicitly[collection.implicitlydocumentproducer](_)) collection.bulkinsert(ordered=true)(bulkdocs: _*)
gives me error hard debug :
type mismatch; found : seq[reactivemongo.play.json.collection.jsoncollection#implicitlydocumentproducer] required: seq[x$48.implicitlydocumentproducer]
i rather not use streams , use second solution, don't have things don't understand in code..
i found how handle bulkinsert. there example
build.sbt
... librarydependencies ++= seq( "org.reactivemongo" %% "play2-reactivemongo" % "0.11.14" ) ...
plugins.sbt
addsbtplugin("com.typesafe.play" % "sbt-plugin" % "2.5.12")
cxtransactionsrepository.scala
package cx.repository import cx.model.cxtransactionentity import play.modules.reactivemongo.reactivemongoapi import reactivemongo.play.json.collection.jsoncollection import scala.concurrent.{executioncontext, future} class cxtransactionsrepository @inject()(val reactivemongoapi: reactivemongoapi)(implicit ec: executioncontext){ private val cxtransactionscollectionfuture: future[jsoncollection] = reactivemongoapi.database.map(_.collection[jsoncollection]("cxtransactions")) def bulkinsert(seq: seq[cxtransactionentity]): future[int] = { { transactions <- cxtransactionscollectionfuture writeresult <- transactions.bulkinsert(ordered = false)(seq.map(implicitly[transactions.implicitlydocumentproducer](_)): _*) } yield { writeresult.n } } }
Comments
Post a Comment