Prepares a flow to orderedly insert batches in the specified collection.
Prepares a flow to orderedly insert batches in the specified collection.
the write parallelism
an optional write concern (if None
the default one is used)
if true bypass the document validation
import akka.NotUsed import akka.stream.Materializer import akka.stream.scaladsl.{ Flow, Sink, Source } import reactivemongo.api.bson.BSONDocument import reactivemongo.api.bson.collection.BSONCollection import reactivemongo.akkastream.Flows def insert( coll: BSONCollection, src: => Source[BSONDocument, NotUsed] )(implicit m: Materializer) = { val flow: Flow[ Iterable[BSONDocument], coll.MultiBulkWriteResult, NotUsed ] = Flows(coll).insertMany[BSONDocument](parallelism = 1) val batchSrc: Source[Iterable[BSONDocument], NotUsed] = src.grouped(128) batchSrc.via(flow).runWith(Sink.fold(0) { (c, res) => c + res.n }) }
Prepares a flow to unorderedly insert batches in the specified collection.
Prepares a flow to unorderedly insert batches in the specified collection.
the write parallelism
an optional write concern (if None
the default one is used)
if true bypass the document validation
import akka.NotUsed import akka.stream.Materializer import akka.stream.scaladsl.{ Sink, Source } import reactivemongo.api.bson.BSONDocumentWriter import reactivemongo.api.bson.collection.BSONCollection import reactivemongo.akkastream.Flows def insertUnordered[T]( coll: BSONCollection, src: => Source[T, NotUsed] )(implicit m: Materializer, w: BSONDocumentWriter[T]) = { val batchSrc: Source[Iterable[T], NotUsed] = src.grouped(128) val builder = Flows(coll) batchSrc.via(builder.insertManyUnordered[T](parallelism = 10)). runWith(Sink.fold(0) { (c, res) => c + res.n }) }
Prepares a flow to orderedly insert documents in the specified collection.
Prepares a flow to orderedly insert documents in the specified collection.
the write parallelism
an optional write concern (if None
the default one is used)
if true bypass the document validation
import akka.NotUsed import akka.stream.Materializer import akka.stream.scaladsl.{ Sink, Source } import reactivemongo.api.bson.BSONDocument import reactivemongo.api.bson.collection.BSONCollection import reactivemongo.akkastream.Flows def insert( coll: BSONCollection, src: => Source[BSONDocument, NotUsed] )(implicit m: Materializer) = { val flow = Flows(coll).insertOne[BSONDocument](parallelism = 1) src.via(flow).runWith(Sink.fold(0) { (c, res) => c + res.n }) }
Prepares a flow to orderedly insert documents in the specified collection.
Prepares a flow to orderedly insert documents in the specified collection.
the write parallelism
an optional write concern (if None
the default one is used)
if true bypass the document validation
import akka.NotUsed import akka.stream.Materializer import akka.stream.scaladsl.{ Sink, Source } import reactivemongo.api.bson.BSONDocument import reactivemongo.api.bson.collection.BSONCollection import reactivemongo.akkastream.Flows def insert( coll: BSONCollection, src: => Source[BSONDocument, NotUsed] )(implicit m: Materializer) = { val flow = Flows(coll).insertOneUnordered[BSONDocument](parallelism = 10) src.via(flow).runWith(Sink.fold(0) { (c, res) => c + res.n }) }
Prepares a flow to orderedly update batches in the specified collection.
Prepares a flow to orderedly update batches in the specified collection.
the write parallelism
an optional write concern (if None
the default one is used)
if true bypass the document validation
the function to prepare each update element from a T
value
reactivemongo.api.collections.GenericCollection.UpdateBuilder.element
import akka.NotUsed import akka.stream.scaladsl.Flow import reactivemongo.api.bson.BSONDocument import reactivemongo.api.bson.collection.BSONCollection import reactivemongo.akkastream.Flows def myUpdateFlow(coll: BSONCollection): Flow[ Iterable[(String, BSONDocument)], coll.MultiBulkWriteResult, NotUsed ] = Flows(coll).updateMany[(String, BSONDocument)](parallelism = 2) { case (upBuilder, (idStr, doc)) => upBuilder.element( q = BSONDocument("_id" -> idStr), u = doc, multi = false, upsert = true) }
Prepares a flow to orderedly update batches in the specified collection.
Prepares a flow to orderedly update batches in the specified collection.
the write parallelism
an optional write concern (if None
the default one is used)
if true bypass the document validation
the function to prepare each update element from a T
value
reactivemongo.api.collections.GenericCollection.UpdateBuilder.element
import scala.concurrent.Future import akka.NotUsed import akka.stream.Materializer import akka.stream.scaladsl.{ Source, Sink } import reactivemongo.api.bson.BSONDocument import reactivemongo.api.bson.collection.BSONCollection import reactivemongo.akkastream.Flows def myUpdate( coll: BSONCollection, src: Source[(String, BSONDocument), NotUsed] )(implicit m: Materializer): Future[Int] = { import scala.language.existentials // required for 'flow' val bellow val flow = Flows(coll). updateManyUnordered[(String, BSONDocument)](parallelism = 2) { case (upBuilder, (idStr, doc)) => upBuilder.element( q = BSONDocument("_id" -> idStr), u = doc, multi = false, upsert = true) } src.grouped(10). // batching 10 per 10 updates via(flow).runWith(Sink.fold(0) { _ + _.n }) }
Prepares a flow to orderedly update documents in the specified collection.
Prepares a flow to orderedly update documents in the specified collection.
the write parallelism
an optional write concern (if None
the default one is used)
if true bypass the document validation
the function to prepare each update element from a T
value
reactivemongo.api.collections.GenericCollection.UpdateBuilder.element
import akka.NotUsed import akka.stream.scaladsl.Flow import reactivemongo.api.bson.BSONDocument import reactivemongo.api.bson.collection.BSONCollection import reactivemongo.akkastream.Flows def myUpdateFlow(coll: BSONCollection): Flow[ (String, BSONDocument), coll.UpdateWriteResult, NotUsed ] = Flows(coll).updateOne[(String, BSONDocument)](parallelism = 1) { case (upBuilder, (idStr, doc)) => upBuilder.element( q = BSONDocument("_id" -> idStr), u = doc, multi = false, upsert = true) }
Prepares a flow to orderedly update batches in the specified collection.
Prepares a flow to orderedly update batches in the specified collection.
the write parallelism
an optional write concern (if None
the default one is used)
if true bypass the document validation
the function to prepare each update element from a T
value
reactivemongo.api.collections.GenericCollection.UpdateBuilder.element
import scala.concurrent.Future import akka.NotUsed import akka.stream.Materializer import akka.stream.scaladsl.{ Source, Sink } import reactivemongo.api.bson.BSONDocument import reactivemongo.api.bson.collection.BSONCollection import reactivemongo.akkastream.Flows def myUpdate( coll: BSONCollection, src: Source[(String, BSONDocument), NotUsed] )(implicit m: Materializer): Future[Int] = { import scala.language.existentials // required for 'flow' val bellow val flow = Flows(coll). updateOneUnordered[(String, BSONDocument)](parallelism = 10) { case (upBuilder, (idStr, doc)) => upBuilder.element( q = BSONDocument("_id" -> idStr), u = doc, multi = false, upsert = true) } src.via(flow).runWith(Sink.fold(0) { _ + _.n }) }
Flow builder to stream data to MongoDB.
the type of the serialization pack of the target collection