Flows

sealed trait Flows[P <: SerializationPack, C <: GenericCollection[P]]

Flow builder to stream data to MongoDB.

Type parameters:
P

the type of the serialization pack of the target collection

Companion:
object
class Object
trait Matchable
class Any

Value members

Concrete methods

def insertMany[T](parallelism: Int, writeConcern: Option[WriteConcern], bypassDocumentValidation: Boolean)(implicit w: Writer[T]): Flow[Iterable[T], MultiBulkWriteResult, NotUsed]

Prepares a flow to orderedly insert batches in the specified collection.

Prepares a flow to orderedly insert batches in the specified collection.

Value parameters:
bypassDocumentValidation

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 })
}
parallelism

the write parallelism

writeConcern

an optional write concern (if None the default one is used)

def insertManyUnordered[T](parallelism: Int, writeConcern: Option[WriteConcern], bypassDocumentValidation: Boolean)(implicit w: Writer[T]): Flow[Iterable[T], MultiBulkWriteResult, NotUsed]

Prepares a flow to unorderedly insert batches in the specified collection.

Prepares a flow to unorderedly insert batches in the specified collection.

Value parameters:
bypassDocumentValidation

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 })
}
parallelism

the write parallelism

writeConcern

an optional write concern (if None the default one is used)

def insertOne[T](parallelism: Int, writeConcern: Option[WriteConcern], bypassDocumentValidation: Boolean)(implicit w: Writer[T]): Flow[T, WriteResult, NotUsed]

Prepares a flow to orderedly insert documents in the specified collection.

Prepares a flow to orderedly insert documents in the specified collection.

Value parameters:
bypassDocumentValidation

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 })
}
parallelism

the write parallelism

writeConcern

an optional write concern (if None the default one is used)

def insertOneUnordered[T](parallelism: Int, writeConcern: Option[WriteConcern], bypassDocumentValidation: Boolean)(implicit w: Writer[T]): Flow[T, WriteResult, NotUsed]

Prepares a flow to orderedly insert documents in the specified collection.

Prepares a flow to orderedly insert documents in the specified collection.

Value parameters:
bypassDocumentValidation

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 })
}
parallelism

the write parallelism

writeConcern

an optional write concern (if None the default one is used)

def updateMany[T](parallelism: Int, writeConcern: Option[WriteConcern], bypassDocumentValidation: Boolean)(element: (UpdateBuilder, T) => Future[UpdateElement]): Flow[Iterable[T], MultiBulkWriteResult, NotUsed]

Prepares a flow to orderedly update batches in the specified collection.

Prepares a flow to orderedly update batches in the specified collection.

Value parameters:
bypassDocumentValidation

if true bypass the document validation

element

the function to prepare each update element from a T value

parallelism

the write parallelism

writeConcern

an optional write concern (if None the default one is used)

See also:

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)
}
def updateManyUnordered[T](parallelism: Int, writeConcern: Option[WriteConcern], bypassDocumentValidation: Boolean)(element: (UpdateBuilder, T) => Future[UpdateElement]): Flow[Iterable[T], MultiBulkWriteResult, NotUsed]

Prepares a flow to orderedly update batches in the specified collection.

Prepares a flow to orderedly update batches in the specified collection.

Value parameters:
bypassDocumentValidation

if true bypass the document validation

element

the function to prepare each update element from a T value

parallelism

the write parallelism

writeConcern

an optional write concern (if None the default one is used)

See also:

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 })
}
def updateOne[T](parallelism: Int, writeConcern: Option[WriteConcern], bypassDocumentValidation: Boolean)(element: (UpdateBuilder, T) => Future[UpdateElement]): Flow[T, UpdateWriteResult, NotUsed]

Prepares a flow to orderedly update documents in the specified collection.

Prepares a flow to orderedly update documents in the specified collection.

Value parameters:
bypassDocumentValidation

if true bypass the document validation

element

the function to prepare each update element from a T value

parallelism

the write parallelism

writeConcern

an optional write concern (if None the default one is used)

See also:

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)
}
def updateOneUnordered[T](parallelism: Int, writeConcern: Option[WriteConcern], bypassDocumentValidation: Boolean)(element: (UpdateBuilder, T) => Future[UpdateElement]): Flow[T, UpdateWriteResult, NotUsed]

Prepares a flow to orderedly update batches in the specified collection.

Prepares a flow to orderedly update batches in the specified collection.

Value parameters:
bypassDocumentValidation

if true bypass the document validation

element

the function to prepare each update element from a T value

parallelism

the write parallelism

writeConcern

an optional write concern (if None the default one is used)

See also:

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 })
}

Abstract fields

val collection: C

The target collection

The target collection