GenericCollection

reactivemongo.api.collections.GenericCollection
trait GenericCollection[P <: SerializationPack] extends Collection with InsertOps[P] with UpdateOps[P] with DeleteOps[P] with FindAndModifyOps[P] with ChangeStreamOps[P] with AggregationOps[P] with QueryBuilderFactory[P]

A Collection that provides default methods using a SerializationPack.

Some methods of this collection accept instances of Reader[T] and Writer[T], that transform any T instance into a document, compatible with the selected serialization pack, and vice-versa.

Attributes

P

the serialization pack

Graph
Supertypes
trait QueryBuilderFactory[P]
trait DeleteOps[P]
trait UpdateOps[P]
trait InsertOps[P]
class Object
trait Matchable
class Any
Self type

Members list

Concise view

Type members

Inherited classlikes

The aggregation framework for this collection

The aggregation framework for this collection

Attributes

Inherited from:
AggregationOps
Graph
Supertypes
class Object
trait Matchable
class Any
final class AggregatorContext[T]

Aggregator context

Aggregator context

Attributes

Inherited from:
AggregationOps
Graph
Supertypes
class Object
trait Matchable
class Any
sealed trait DeleteBuilder

Builder for delete operations.

Builder for delete operations.

Attributes

Inherited from:
DeleteOps
Graph
Supertypes
class Object
trait Matchable
class Any
final class DeleteElement

Delete command element

Delete command element

Attributes

Inherited from:
DeleteCommand (hidden)
Graph
Supertypes
class Object
trait Matchable
class Any
protected case class DistinctResult(values: Iterable[Value])

Attributes

values

the raw values (should not contain duplicate)

Inherited from:
DistinctOp (hidden)
Graph
Supertypes
trait Serializable
trait Product
trait Equals
class Object
trait Matchable
class Any
sealed trait FindAndModifyOp

A modify operation, part of a FindAndModify command

A modify operation, part of a FindAndModify command

Attributes

Inherited from:
FindAndModifyCommand
Graph
Supertypes
class Object
trait Matchable
class Any
Known subtypes

Result for a find-and-modify command

Result for a find-and-modify command

Attributes

Inherited from:
FindAndModifyCommand
Graph
Supertypes
class Object
trait Matchable
class Any

Remove (part of a FindAndModify command).

Remove (part of a FindAndModify command).

Attributes

Inherited from:
FindAndModifyCommand
Graph
Supertypes
class Object
trait Matchable
class Any

Last error for a find-and-update command

Last error for a find-and-update command

Attributes

Inherited from:
FindAndModifyCommand
Graph
Supertypes
class Object
trait Matchable
class Any
final class FindAndUpdateOp extends FindAndModifyOp

Update (part of a FindAndModify command).

Update (part of a FindAndModify command).

Attributes

fetchNewObject

the command result must be the new object instead of the old one.

update

the modifier document.

upsert

if true, creates a new document if no document matches the query, or if documents match the query, findAndModify performs an update

Inherited from:
FindAndModifyCommand
Graph
Supertypes
class Object
trait Matchable
class Any

Update (part of a FindAndModify command).

Update (part of a FindAndModify command).

Attributes

fetchNewObject

the command result must be the new object instead of the old one.

update

the aggregation pipeline

upsert

if true, creates a new document if no document matches the query, or if documents match the query, findAndModify performs an update

Inherited from:
FindAndModifyCommand
Graph
Supertypes
class Object
trait Matchable
class Any
sealed trait Hint

An index hint

An index hint

Attributes

Inherited from:
HintFactory (hidden)
Graph
Supertypes
class Object
trait Matchable
class Any
sealed trait InsertBuilder

Builder for insert operations.

Builder for insert operations.

Attributes

Inherited from:
InsertOps
Graph
Supertypes
class Object
trait Matchable
class Any

The result of a bulk write operation.

The result of a bulk write operation.

Attributes

Inherited from:
MultiBulkWriteResultFactory
Graph
Supertypes
class Object
trait Matchable
class Any
final class QueryBuilder

A builder that helps to make a fine-tuned query to MongoDB.

A builder that helps to make a fine-tuned query to MongoDB.

When the query is ready, you can call cursor to get a Cursor, or one if you want to retrieve just one document.

Attributes

batchSize

the upper limit on the number of documents to retrieve per batch (0 for unspecified)

collation

the optional collation to use for the find command (default: None) {@since MongoDB 3.4}

import scala.concurrent.{ ExecutionContext, Future }
import reactivemongo.api.bson.BSONDocument
import reactivemongo.api.bson.collection.BSONCollection
def firstFirst(coll: BSONCollection)(
 implicit ec: ExecutionContext): Future[Option[BSONDocument]] = {
 val queryBuilder = coll.find(BSONDocument.empty)
 queryBuilder.one[BSONDocument]
}
comment

the query comment

cursorOptions

the cursor options

explain

the explain flag

filter

the query filter

hint

the index to “hint” or force MongoDB to use when performing the query.

max

the optional exclusive upper bound for a specific index (default: None)

maxScan

this option specifies a maximum number of documents or index keys the query plan will scan.

maxTimeMs

the maximum execution time

min

the optional exclusive lower bound for a specific index (default: None)

projection

the projection specification

readConcern

the read concern {@since MongoDB 3.6}

readPreference

the query ReadPreference

returnKey

if this flag is true, returns only the index keys in the resulting documents

showRecordId

the flags to determines whether to return the record identifier for each document

singleBatch

the flag to determines whether to close the cursor after the first batch (default: false)

skip

the number of documents to skip.

snapshot

the snapshot flag

sort

the optional sort specification

Inherited from:
QueryBuilderFactory
Graph
Supertypes
class Object
trait Matchable
class Any
sealed trait UpdateBuilder

Builder for update operations.

Builder for update operations.

Attributes

Inherited from:
UpdateOps
Graph
Supertypes
class Object
trait Matchable
class Any
final class UpdateElement(val q: Document, val u: Either[Document, Seq[Document]], val upsert: Boolean, val multi: Boolean, val collation: Option[Collation], val arrayFilters: Seq[Document])

Attributes

arrayFilters

an array of filter documents that determines which array elements to modify for an update operation on an array field

collation

the collation to use for the operation

multi

if true updates all the matching documents

q

the query that matches the documents to update

u

the modifications to apply

upsert

if true perform an insert if no documents match the query

Inherited from:
UpdateCommand (hidden)
Graph
Supertypes
class Object
trait Matchable
class Any
final class UpdateWriteResult extends WriteResult

Result for the update operations.

Result for the update operations.

Attributes

n

the number of documents selected for update

nModified

the number of updated documents

ok

the update status

upserted

the upserted documents

Inherited from:
UpdateWriteResultFactory (hidden)
Graph
Supertypes
class Object
trait Matchable
class Any
final class Upserted

An upserted element

An upserted element

Attributes

Inherited from:
UpsertedFactory
Graph
Supertypes
class Object
trait Matchable
class Any
object Upserted

Attributes

Inherited from:
UpsertedFactory
Graph
Supertypes
class Object
trait Matchable
class Any
sealed trait WatchBuilder[T]

A builder for the watch collection helper, which allows to consume the collection's ChangeStream.

A builder for the watch collection helper, which allows to consume the collection's ChangeStream.

Attributes

Inherited from:
ChangeStreamOps
Graph
Supertypes
class Object
trait Matchable
class Any

Inherited types

Aggregation framework

Aggregation framework

Attributes

Inherited from:
AggregationOps
final type DeleteResult = DefaultWriteResult

Result for a delete command

Result for a delete command

Attributes

Inherited from:
DeleteCommand (hidden)

Aggregation pipeline operator/stage

Aggregation pipeline operator/stage

Attributes

Inherited from:
AggregationOps

Value members

Abstract methods

The default read preference

The default read preference

Attributes

Returns a new reference to the same collection, with the given read preference.

Returns a new reference to the same collection, with the given read preference.

Attributes

Concrete methods

def aggregateWith[T](explain: Boolean, allowDiskUse: Boolean, bypassDocumentValidation: Boolean, readConcern: ReadConcern, readPreference: ReadPreference, batchSize: Option[Int])(f: AggregationFramework => Pipeline)(implicit reader: Reader[T], cp: CursorProducer[T]): ProducedCursor

Aggregates the matching documents.

Aggregates the matching documents.

Attributes

T

The type of the result elements. An implicit Reader[T] typeclass for handling it has to be in the scope.

allowDiskUse

if true enables writing to temporary files

batchSize

the batch size (for the aggregation cursor; if None use the default one)

bypassDocumentValidation

the flag to bypass document validation during the operation (default: false)

explain

if true indicates to return the information on the processing of the pipeline

f

the function to create the aggregation pipeline using the aggregation framework depending on the collection type

readConcern

the read concern

readPreference

the read preference for the result (default: primary)

reader

the result reader

def aggregationUpdateModifier(update: Pipeline, fetchNewObject: Boolean, upsert: Boolean): FindAndUpdateWithAggregateOp

'''EXPERIMENTAL:''' Returns an update modifier, to be used with findAndModify.

'''EXPERIMENTAL:''' Returns an update modifier, to be used with findAndModify.

Attributes

fetchNewObject

the command result must be the new object instead of the old one

update

the aggregation pipeline

upsert

if true, creates a new document if no document is matching, otherwise if at least one document matches, an update is applied

def count(selector: Option[Document], limit: Option[Int], skip: Int, hint: Option[Hint], readConcern: ReadConcern, readPreference: ReadPreference)(implicit ec: ExecutionContext): Future[Long]

Counts the matching documents.

Counts the matching documents.

Attributes

hint

the index to use (either the index name or the index document; see hint(..))

limit

the maximum number of matching documents to count

readConcern

the read concern

readPreference

the read preference for the result

selector

the document selector

skip

the number of matching documents to skip before counting

See also:

Prepares an unordered delete builder.

Prepares an unordered delete builder.

import scala.concurrent.ExecutionContext

import reactivemongo.api.bson.BSONDocument
import reactivemongo.api.bson.collection.BSONCollection

def doIt(coll: BSONCollection, query: BSONDocument)(
 implicit ec: ExecutionContext) = coll.delete.one(query)

def equivalentTo(coll: BSONCollection) = coll.delete(false)

Attributes

def delete(ordered: Boolean, writeConcern: WriteConcern): DeleteBuilder

Prepares a delete builder.

Prepares a delete builder.

Attributes

ordered

the ordered behaviour

writeConcern

the writer concern to be used

import scala.concurrent.ExecutionContext
import reactivemongo.api.bson.BSONDocument
import reactivemongo.api.bson.collection.BSONCollection
def foo(coll: BSONCollection, query: BSONDocument)(
 implicit ec: ExecutionContext) = coll.delete(true).one(query)
def find[S](selector: S)(implicit swriter: Writer[S]): QueryBuilder

Finds the documents matching the given criteria (selector), with the projection applied.

Finds the documents matching the given criteria (selector), with the projection applied.

Attributes

P

The type of the projection object. An implicit Writer[P] typeclass for handling it has to be in the scope.

S

The type of the selector. An implicit Writer[S] typeclass for handling it has to be in the scope.

pwriter

the writer for the projection

selector

the document selector

swriter

the writer for the selector

Returns:

A query builder you can use to customize the query. You can obtain a cursor by calling the method reactivemongo.api.Cursor on this query builder.

See also:
def find[S, J](selector: S, projection: Option[J])(implicit swriter: Writer[S], pwriter: Writer[J]): QueryBuilder

Finds the documents matching the given criteria (selector), with the projection applied.

Finds the documents matching the given criteria (selector), with the projection applied.

Attributes

P

The type of the projection object. An implicit Writer[P] typeclass for handling it has to be in the scope.

S

The type of the selector. An implicit Writer[S] typeclass for handling it has to be in the scope.

projection

the projection document to select only a subset of each matching documents

pwriter

the writer for the projection

selector

the document selector

swriter

the writer for the selector

Returns:

A query builder you can use to customize the query. You can obtain a cursor by calling the method reactivemongo.api.Cursor on this query builder.

See also:
def findAndModify[S](selector: S, modifier: FindAndModifyOp, sort: Option[Document], fields: Option[Document], bypassDocumentValidation: Boolean, writeConcern: WriteConcern, maxTime: Option[FiniteDuration], collation: Option[Collation], arrayFilters: Seq[Document])(implicit swriter: Writer[S], ec: ExecutionContext): Future[FindAndModifyResult]

Applies a findAndModify operation. See findAndUpdate and findAndRemove convenient functions.

Applies a findAndModify operation. See findAndUpdate and findAndRemove convenient functions.

import scala.concurrent.{ ExecutionContext, Future }

import reactivemongo.api.bson.{ BSONDocument, BSONDocumentReader }
import reactivemongo.api.bson.collection.BSONCollection

case class Person(name: String, age: Int)

def foo(coll: BSONCollection)(
 implicit ec: ExecutionContext, r: BSONDocumentReader[Person]) = {
 val updateOp = coll.updateModifier(
   BSONDocument(f"$$set" -> BSONDocument("age" -> 35)))

 val personBeforeUpdate: Future[Option[Person]] =
   coll.findAndModify(BSONDocument("name" -> "Joline"), updateOp).
   map(_.result[Person])

 val removedPerson: Future[Option[Person]] = coll.findAndModify(
   BSONDocument("name" -> "Jack"), coll.removeModifier).
   map(_.result[Person])

 val _ = List(personBeforeUpdate, removedPerson)
}

Attributes

arrayFilters

an array of filter documents that determines which array elements to modify for an update operation on an array field

bypassDocumentValidation

the flag to bypass document validation during the operation (default: false)

collation

the collation

fields

the projection fields

maxTime

the time limit for processing operations on a cursor (maxTimeMS)

modifier

the modify operator to be applied

selector

the document selector

sort

the document indicating the sort criteria (default: None)

swriter

the writer for the selector

tparam

S The type of the selector. An implicit Writer[S] typeclass for handling it has to be in the scope.

writeConcern

the writer concern to be used

def findAndRemove[S](selector: S, sort: Option[Document], fields: Option[Document], writeConcern: WriteConcern, maxTime: Option[FiniteDuration], collation: Option[Collation], arrayFilters: Seq[Document])(implicit swriter: Writer[S], ec: ExecutionContext): Future[FindAndModifyResult]

Finds some matching document, and removes it (using findAndModify).

Finds some matching document, and removes it (using findAndModify).

import scala.concurrent.{ ExecutionContext, Future }

import reactivemongo.api.bson.{ BSONDocument, BSONDocumentReader }
import reactivemongo.api.bson.collection.BSONCollection

case class Person(name: String, age: Int)

def removed(coll: BSONCollection)(
 implicit ec: ExecutionContext,
 r: BSONDocumentReader[Person]): Future[Option[Person]] =
 coll.findAndRemove(
   BSONDocument("name" -> "Foo")).map(_.result[Person])

Attributes

S

The type of the selector. An implicit Writer[S] typeclass for handling it has to be in the scope.

arrayFilters

an array of filter documents that determines which array elements to modify for an update operation on an array field

collation

the collation

fields

the projection fields

maxTime

the time limit for processing operations on a cursor (maxTimeMS)

modifier

the modify operator to be applied

selector

the document selector

sort

the document indicating the sort criteria

swriter

the writer for the selector

writeConcern

the writer concern to be used

def findAndUpdate[S, T](selector: S, update: T, fetchNewObject: Boolean, upsert: Boolean, sort: Option[Document], fields: Option[Document], bypassDocumentValidation: Boolean, writeConcern: WriteConcern, maxTime: Option[FiniteDuration], collation: Option[Collation], arrayFilters: Seq[Document])(implicit swriter: Writer[S], writer: Writer[T], ec: ExecutionContext): Future[FindAndModifyResult]

Finds some matching document, and updates it (using findAndModify).

Finds some matching document, and updates it (using findAndModify).

import scala.concurrent.{ ExecutionContext, Future }

import reactivemongo.api.bson.BSONDocument
import reactivemongo.api.bson.collection.BSONCollection

def findPerson(coll: BSONCollection)(
 implicit ec: ExecutionContext): Future[Option[BSONDocument]] =
 coll.findAndUpdate(
   BSONDocument("name" -> "James"),
   BSONDocument(f"$$set" -> BSONDocument("age" -> 17)),
   fetchNewObject = true).map(_.value)
   // on success, return the update document: { "age": 17 }

Attributes

arrayFilters

an array of filter documents that determines which array elements to modify for an update operation on an array field

bypassDocumentValidation

the flag to bypass document validation during the operation (default: false)

collation

the collation

fetchNewObject

the command result must be the new object instead of the old one (default: false)

fields

the projection fields

maxTime

the time limit for processing operations on a cursor (maxTimeMS)

selector

the document selector

sort

the document indicating the sort criteria (default: None)

swriter

the writer for the selector

update

the update to be applied

upsert

if true, creates a new document if no document is matching, otherwise if at least one document matches, an update is applied (default: false)

writeConcern

the writer concern to be used

writer

the writer to create the document

def findAndUpdateWithPipeline[S](selector: S, update: Pipeline, fetchNewObject: Boolean, upsert: Boolean, sort: Option[Document], fields: Option[Document], bypassDocumentValidation: Boolean, writeConcern: WriteConcern, maxTime: Option[FiniteDuration], collation: Option[Collation], arrayFilters: Seq[Document])(implicit swriter: Writer[S], ec: ExecutionContext): Future[FindAndModifyResult]

'''EXPERIMENTAL:''' Finds some matching document, and updates it (using findAndModify).

'''EXPERIMENTAL:''' Finds some matching document, and updates it (using findAndModify).

import scala.concurrent.{ ExecutionContext, Future }

import reactivemongo.api.bson.{ BSONArray, BSONDocument }
import reactivemongo.api.bson.collection.BSONCollection

def findPerson(coll: BSONCollection)(
 implicit ec: ExecutionContext): Future[Option[BSONDocument]] = {
 import coll.AggregationFramework.{ PipelineOperator, Set }

 val pipeline = List[PipelineOperator](
   Set(BSONDocument("age" -> BSONDocument(
     f"$$add" -> BSONArray(f"$$age", 2)))))

 coll.findAndUpdateWithPipeline(
   BSONDocument("name" -> "James"),
   pipeline,
   fetchNewObject = true).map(_.value)
   // on success, return the update document: { "age": 17 }
}

Attributes

arrayFilters

an array of filter documents that determines which array elements to modify for an update operation on an array field

bypassDocumentValidation

the flag to bypass document validation during the operation (default: false)

collation

the collation

fetchNewObject

the command result must be the new object instead of the old one (default: false)

fields

the projection fields

maxTime

the time limit for processing operations on a cursor (maxTimeMS)

selector

the document selector

sort

the document indicating the sort criteria (default: None)

swriter

the writer for the selector

update

the aggregation pipeline

upsert

if true, creates a new document if no document is matching, otherwise if at least one document matches, an update is applied (default: false)

writeConcern

the writer concern to be used

writer

the writer to create the document

Returns an unordered builder for insert operations. Uses the default write concern.

Returns an unordered builder for insert operations. Uses the default write concern.

Attributes

T

The type of the document to insert. An implicit Writer[T] typeclass for handling it has to be in the scope.

ordered

the ordered behaviour

import scala.concurrent.ExecutionContext
import reactivemongo.api.bson.BSONDocument
import reactivemongo.api.bson.collection.BSONCollection
def one(coll: BSONCollection, singleDoc: BSONDocument)(
 implicit ec: ExecutionContext) =
 coll.insert.one(singleDoc)
def many(coll: BSONCollection, multiInserts: Iterable[BSONDocument])(
 implicit ec: ExecutionContext) =
 coll.insert.many(multiInserts)

Returns a builder for insert operations. Uses the default write concern.

Returns a builder for insert operations. Uses the default write concern.

Attributes

T

The type of the document to insert. An implicit Writer[T] typeclass for handling it has to be in the scope.

ordered

the ordered behaviour

import scala.concurrent.ExecutionContext
import reactivemongo.api.bson.BSONDocument
import reactivemongo.api.bson.collection.BSONCollection
def one(coll: BSONCollection, singleDoc: BSONDocument)(
 implicit ec: ExecutionContext) =
 coll.insert(ordered = true).one(singleDoc)
def many(coll: BSONCollection, multiInserts: Iterable[BSONDocument])(
 implicit ec: ExecutionContext) =
 coll.insert(ordered = true).many(multiInserts)
def insert(writeConcern: WriteConcern): InsertBuilder

Returns an ordered builder for insert operations.

Returns an ordered builder for insert operations.

Attributes

T

The type of the document to insert. An implicit Writer[T] typeclass for handling it has to be in the scope.

writeConcern

the writer concern to be used

import scala.concurrent.ExecutionContext
import reactivemongo.api.WriteConcern
import reactivemongo.api.bson.BSONDocument
import reactivemongo.api.bson.collection.BSONCollection
def one(coll: BSONCollection, singleDoc: BSONDocument)(
 implicit ec: ExecutionContext) =
 coll.insert(writeConcern = WriteConcern.Default).one(singleDoc)
def many(coll: BSONCollection, multiInserts: Iterable[BSONDocument])(
 implicit ec: ExecutionContext) =
 coll.insert(writeConcern = WriteConcern.Default).many(multiInserts)
def insert(ordered: Boolean, writeConcern: WriteConcern): InsertBuilder

Returns a builder for insert operations.

Returns a builder for insert operations.

Attributes

T

The type of the document to insert. An implicit Writer[T] typeclass for handling it has to be in the scope.

ordered

the ordered behaviour

writeConcern

the writer concern to be used

import scala.concurrent.ExecutionContext
import reactivemongo.api.WriteConcern
import reactivemongo.api.bson.BSONDocument
import reactivemongo.api.bson.collection.BSONCollection
def withDefaultWriteConcern(coll: BSONCollection, query: BSONDocument)(
 implicit ec: ExecutionContext) =
 coll.insert(true, WriteConcern.Journaled).one(query)
def insert(ordered: Boolean, bypassDocumentValidation: Boolean): InsertBuilder

Returns a builder for insert operations. Uses the default write concern.

Returns a builder for insert operations. Uses the default write concern.

Attributes

T

The type of the document to insert. An implicit Writer[T] typeclass for handling it has to be in the scope.

bypassDocumentValidation

the flag to bypass document validation during the operation

import scala.concurrent.ExecutionContext
import reactivemongo.api.bson.BSONDocument
import reactivemongo.api.bson.collection.BSONCollection
def withDefaultWriteConcern(coll: BSONCollection, query: BSONDocument)(
 implicit ec: ExecutionContext) =
 coll.insert(true, false).one(query)
ordered

the ordered behaviour

def insert(ordered: Boolean, writeConcern: WriteConcern, bypassDocumentValidation: Boolean): InsertBuilder

Returns a builder for insert operations.

Returns a builder for insert operations.

Attributes

T

The type of the document to insert. An implicit Writer[T] typeclass for handling it has to be in the scope.

bypassDocumentValidation

the flag to bypass document validation during the operation

import scala.concurrent.ExecutionContext
import reactivemongo.api.WriteConcern
import reactivemongo.api.bson.BSONDocument
import reactivemongo.api.bson.collection.BSONCollection
def withDefaultWriteConcern(coll: BSONCollection, query: BSONDocument)(
 implicit ec: ExecutionContext) =
 coll.insert(true, WriteConcern.Journaled, true).one(query)
ordered

the ordered behaviour

writeConcern

the writer concern to be used

protected def readConcern: ReadConcern

The default read concern

The default read concern

Attributes

Returns a removal modifier, to be used with findAndModify.

Returns a removal modifier, to be used with findAndModify.

Attributes

override def toString: String

Returns a string representation of the object.

Returns a string representation of the object.

The default representation is platform dependent.

Attributes

Returns:

a string representation of the object.

Definition Classes
Any
protected def unsupportedVersion(metadata: ProtocolMetadata): GenericDriverException

Returns an unordered update builder. Uses the default write concern.

Returns an unordered update builder. Uses the default write concern.

import scala.concurrent.ExecutionContext

import reactivemongo.api.bson.BSONDocument
import reactivemongo.api.bson.collection.BSONCollection

def withDefaultWriteConcern(
 coll: BSONCollection,
 query: BSONDocument,
 update: BSONDocument
)(implicit ec: ExecutionContext) = {
 coll.update.one(query, update, upsert = false, multi = false)
}

Attributes

Returns an update builder. Uses the default write concern.

Returns an update builder. Uses the default write concern.

import scala.concurrent.ExecutionContext

import reactivemongo.api.bson.BSONDocument
import reactivemongo.api.bson.collection.BSONCollection

def withDefaultWriteConcern(
 coll: BSONCollection,
 query: BSONDocument,
 update: BSONDocument
)(implicit ec: ExecutionContext) = {
 coll.update(ordered = true).
   one(query, update, upsert = false, multi = false)
}

Attributes

ordered

the ordered behaviour

def update(writeConcern: WriteConcern): UpdateBuilder

Returns an ordered update builder.

Returns an ordered update builder.

import scala.concurrent.ExecutionContext

import reactivemongo.api.WriteConcern
import reactivemongo.api.bson.BSONDocument
import reactivemongo.api.bson.collection.BSONCollection

def orderedUpdate(
 coll: BSONCollection,
 query: BSONDocument,
 update: BSONDocument,
 wc: WriteConcern
)(implicit ec: ExecutionContext) =
 coll.update(writeConcern = wc).one(query, update)

Attributes

writeConcern

the writer concern to be used

def update(ordered: Boolean, writeConcern: WriteConcern): UpdateBuilder

Returns an update builder.

Returns an update builder.

import scala.concurrent.ExecutionContext

import reactivemongo.api.WriteConcern
import reactivemongo.api.bson.BSONDocument
import reactivemongo.api.bson.collection.BSONCollection

def myUpdate(
 coll: BSONCollection,
 query: BSONDocument,
 update: BSONDocument,
 wc: WriteConcern
)(implicit ec: ExecutionContext) =
 coll.update(ordered = false, writeConcern = wc).one(query, update)

Attributes

ordered

the ordered behaviour

writeConcern

the writer concern to be used

def update(ordered: Boolean, bypassDocumentValidation: Boolean): UpdateBuilder

Returns an update builder. Uses the default write concern.

Returns an update builder. Uses the default write concern.

import scala.concurrent.ExecutionContext

import reactivemongo.api.bson.BSONDocument
import reactivemongo.api.bson.collection.BSONCollection

def withDefaultWriteConcern(
 coll: BSONCollection,
 query: BSONDocument,
 update: BSONDocument
)(implicit ec: ExecutionContext) = coll.update(
 ordered = false,
 bypassDocumentValidation = true
).one(query, update)

Attributes

bypassDocumentValidation

the flag to bypass document validation during the operation

ordered

the ordered behaviour

writeConcern

the writer concern to be used

def update(ordered: Boolean, writeConcern: WriteConcern, bypassDocumentValidation: Boolean): UpdateBuilder

Returns an update builder.

Returns an update builder.

import scala.concurrent.ExecutionContext

import reactivemongo.api.WriteConcern
import reactivemongo.api.bson.BSONDocument
import reactivemongo.api.bson.collection.BSONCollection

def myUpdate(
 coll: BSONCollection,
 query: BSONDocument,
 update: BSONDocument,
 wc: WriteConcern
)(implicit ec: ExecutionContext) = coll.update(
 ordered = false,
 writeConcern = wc,
 bypassDocumentValidation = true
).one(query, update)

Attributes

bypassDocumentValidation

the flag to bypass document validation during the operation

ordered

the ordered behaviour

writeConcern

the writer concern to be used

def updateModifier[U](update: U, fetchNewObject: Boolean, upsert: Boolean)(implicit updateWriter: Writer[U]): FindAndUpdateOp

Returns an update modifier, to be used with findAndModify.

Returns an update modifier, to be used with findAndModify.

Attributes

fetchNewObject

the command result must be the new object instead of the old one

update

the update to be applied

upsert

if true, creates a new document if no document is matching, otherwise if at least one document matches, an update is applied

protected def watchFailure[T](future: => Future[T]): Future[T]
protected def writeConcern: WriteConcern

The default write concern

The default write concern

Attributes

The read preference for the write operations (primary)

The read preference for the write operations (primary)

Attributes

Inherited methods

def aggregatorContext[T](pipeline: List[PipelineOperator], explain: Boolean, allowDiskUse: Boolean, bypassDocumentValidation: Boolean, readConcern: ReadConcern, readPreference: ReadPreference, writeConcern: WriteConcern, batchSize: Option[Int], cursorOptions: CursorOptions, maxAwaitTime: Option[FiniteDuration], hint: Option[Hint], comment: Option[String], collation: Option[Collation])(implicit reader: Reader[T]): AggregatorContext[T]

Aggregates the matching documents.

Aggregates the matching documents.

import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global

import reactivemongo.api.Cursor
import reactivemongo.api.bson._
import reactivemongo.api.bson.collection.BSONCollection

def populatedStates(cities: BSONCollection): Future[List[BSONDocument]] = {
 import cities.AggregationFramework
 import AggregationFramework.{ Group, Match, SumField }

 cities.aggregatorContext[BSONDocument](
   List(Group(BSONString(f"$$state"))(
     "totalPop" -> SumField("population")),
       Match(BSONDocument("totalPop" ->
         BSONDocument(f"$$gte" -> 10000000L))))
 ).prepared.cursor.collect[List](
   maxDocs = 3,
   err = Cursor.FailOnError[List[BSONDocument]]()
 )
}

Attributes

T

The type of the result elements. An implicit Reader[T] typeclass for handling it has to be in the scope.

allowDiskUse

if true enables writing to temporary files (default: false)

batchSize

the batch size (for the aggregation cursor; if None use the default one)

bypassDocumentValidation

the flag to bypass document validation during the operation (default: false)

collation

the collation

comment

the comment to annotation the aggregation command

cp

the cursor producer

cursor

aggregation cursor option (optional)

cursorOptions

the options for the result cursor

explain

if true indicates to return the information on the processing of the pipeline (default: false)

hint

the index to use (either the index name or the index document)

maxAwaitTime

In practice, this parameter controls the duration of the long-polling behavior of the cursor.

pipeline

the aggregation pipeline

readConcern

the read concern

readPreference

$readPrefParam

reader

the result reader

writeConcern

the writer concern to be used

Inherited from:
GenericCollectionWithAggregatorContext (hidden)
final def convertToCapped(size: Long, maxDocuments: Option[Int])(implicit ec: ExecutionContext): Future[Unit]

Converts this collection to a capped one.

Converts this collection to a capped one.

import scala.concurrent.{ ExecutionContext, Future }

def capColl(coll: reactivemongo.api.bson.collection.BSONCollection)(
 implicit ec: ExecutionContext): Future[Unit] =
 coll.convertToCapped(size = 10L, maxDocuments = Some(100))

Attributes

maxDocuments

the maximum number of documents this capped collection can contain

size

the size of the collection (number of bytes)

Inherited from:
CollectionMetaCommands (hidden)
final def create(failsIfExists: Boolean, writeConcern: WriteConcern)(implicit ec: ExecutionContext): Future[Unit]

Creates this collection.

Creates this collection.

import scala.concurrent.{ ExecutionContext, Future }
import reactivemongo.api.collections.GenericCollection

def createIfNotExists(coll: GenericCollection[_])(
 implicit ec: ExecutionContext): Future[Unit] =
 coll.create(failsIfExists = true)

Attributes

failsIfExists

if true fails if the collection already exists (default: false)

writeConcern

the write concern for collection operations

Inherited from:
CollectionMetaCommands (hidden)
final def create()(implicit ec: ExecutionContext): Future[Unit]

Creates this collection.

Creates this collection.

The returned future will be completed, with an error if this collection already exists.

import scala.concurrent.ExecutionContext

import reactivemongo.api.collections.GenericCollection
import reactivemongo.api.commands.CommandException

def createColl(
 coll: GenericCollection[_])(implicit ec: ExecutionContext) =
 coll.create().recover {
   case CommandException.Code(48) => // NamespaceExists
     println(s"Collection \${coll} already exists")
 }

Attributes

Inherited from:
CollectionMetaCommands (hidden)
final def createCapped(size: Long, maxDocuments: Option[Int])(implicit ec: ExecutionContext): Future[Unit]

Creates this collection as a capped one.

Creates this collection as a capped one.

The returned future will be completed with an error if this collection already exists.

import scala.concurrent.{ ExecutionContext, Future }

def capped(coll: reactivemongo.api.bson.collection.BSONCollection)(
 implicit ec: ExecutionContext): Future[Unit] =
 coll.createCapped(size = 10, maxDocuments = Some(100))

Attributes

maxDocuments

the maximum number of documents this capped collection can contain

size

the size of the collection (number of bytes)

See also:

convertToCapped

Inherited from:
CollectionMetaCommands (hidden)
def createView(name: String, operator: PipelineOperator, pipeline: Seq[PipelineOperator], collation: Option[Collation])(implicit ec: ExecutionContext): Future[Unit]

Creates a view on this collection, using an aggregation pipeline.

Creates a view on this collection, using an aggregation pipeline.

Attributes

collation

the view collation

name

the name of the view to be created

operator

the first (required) operator for the aggregation pipeline

pipeline

the other pipeline operators

Since:

MongoDB 3.4

import scala.concurrent.ExecutionContext
import reactivemongo.api.bson.{ BSONDocument, BSONString }
import reactivemongo.api.bson.collection.BSONCollection
def foo(coll: BSONCollection)(implicit ec: ExecutionContext) = {
 import coll.AggregationFramework
 import AggregationFramework.{ Group, Match, SumField }
 // See http://docs.mongodb.org/manual/tutorial/aggregation-zip-code-data-set/#return-states-with-populations-above-10-million
 // Create 'myview'
 coll.createView(
   name = "myview",
   operator = Group(BSONString(f"$$state"))(
     "totalPop" -> SumField("population")),
   pipeline = Seq(Match(
     BSONDocument("totalPop" -> BSONDocument(f"$$gte" -> 10000000L)))))
 // Then the view can be resolved as any collection
 // (but won't be writeable)
 coll.db[BSONCollection]("myview")
}
Inherited from:
GenericCollectionMetaCommands (hidden)
def db: DB

The database which this collection belongs to.

The database which this collection belongs to.

Attributes

Inherited from:
Collection
final protected def deleteWriter(session: Option[Session]): Writer[DeleteCmd]

Attributes

Inherited from:
DeleteCommand (hidden)
def distinct[T, M <: (Iterable)](key: String, selector: Option[Document], readConcern: ReadConcern, collation: Option[Collation])(implicit reader: NarrowValueReader[T], ec: ExecutionContext, cbf: Factory[T, M[T]]): Future[M[T]]

Returns the distinct values for a specified field across a single collection.

Returns the distinct values for a specified field across a single collection.

Attributes

M

the container, that must be a scala.collection.Iterable

T

the element type of the distinct values

collation

$collationParam

import scala.concurrent.ExecutionContext
import reactivemongo.api.ReadConcern
import reactivemongo.api.bson.collection.BSONCollection
def distinctStates(coll: BSONCollection)(implicit ec: ExecutionContext) =
 coll.distinct[String, Set]("state", None, ReadConcern.Local, None)
key

the field for which to return distinct values

readConcern

$readConcernParam

selector

$selectorParam, that specifies the documents from which to retrieve the distinct values.

Inherited from:
GenericCollectionWithDistinctOps (hidden)
protected def distinctDocuments[T, M <: (Iterable)](key: String, query: Option[Document], readConcern: ReadConcern, collation: Option[Collation], builder: Builder[T, M[T]])(implicit reader: NarrowValueReader[T], ec: ExecutionContext): Future[M[T]]

Attributes

Inherited from:
DistinctOp (hidden)
final protected def distinctDocuments[T, M <: (Iterable)](key: String, query: Option[Document], readConcern: ReadConcern, collation: Option[Collation])(implicit reader: NarrowValueReader[T], ec: ExecutionContext, cbf: Factory[T, M[T]]): Future[M[T]]

Attributes

Inherited from:
DistinctOpCompat (hidden)
final def drop(failIfNotFound: Boolean)(implicit ec: ExecutionContext): Future[Boolean]

Drops this collection.

Drops this collection.

If the collection existed and is successfully dropped, the returned future will be completed with true.

If failIfNotFound is false and the collection doesn't exist, the returned future will be completed with false.

Otherwise in case, the future will be completed with the encountered error.

import scala.concurrent.{ ExecutionContext, Future }
import reactivemongo.api.collections.GenericCollection

def dropIfNotFound(coll: GenericCollection[_])(
 implicit ec: ExecutionContext): Future[Boolean] =
 coll.drop(failIfNotFound = true)

Attributes

failIfNotFound

the flag to request whether it should fail

Inherited from:
CollectionMetaCommands (hidden)
def drop()(implicit ec: ExecutionContext): Future[Unit]

Drops this collection.

Drops this collection.

The returned future will be completed with an error if this collection does not exist.

Attributes

Inherited from:
CollectionMetaCommands (hidden)
def hint(specification: Document): Hint

Returns a hint for the given index specification document.

Returns a hint for the given index specification document.

Attributes

specification

the index specification document

Inherited from:
HintFactory (hidden)
def hint(name: String): Hint

Returns a hint for the given index name.

Returns a hint for the given index name.

Attributes

name

the index name

Inherited from:
HintFactory (hidden)
final def indexesManager(implicit ec: ExecutionContext): Aux[Pack]

Returns an index manager for this collection.

Returns an index manager for this collection.

import scala.concurrent.{ ExecutionContext, Future }
import reactivemongo.api.collections.GenericCollection

def listIndexes(coll: GenericCollection[_])(
 implicit ec: ExecutionContext): Future[List[String]] =
 coll.indexesManager.list().map(_.flatMap { idx =>
   idx.name.toList
 })

Attributes

Inherited from:
CollectionMetaCommands (hidden)

The name of the collection.

The name of the collection.

Attributes

Inherited from:
Collection
def runCommand[C <: CollectionCommand](command: C)(implicit writer: Writer[ResolvedCollectionCommand[C]]): CursorFetcher[P, Cursor]

Attributes

Inherited from:
GenericCollectionWithCommands (hidden)
def runCommand[R, C <: CollectionCommand & CommandWithResult[R]](command: C & CommandWithResult[R], readPreference: ReadPreference)(implicit writer: Writer[ResolvedCollectionCommand[C]], reader: Reader[R], ec: ExecutionContext): Future[R]

Attributes

Inherited from:
GenericCollectionWithCommands (hidden)
def runWithResponse[R, C <: CollectionCommand & CommandWithResult[R]](command: C & CommandWithResult[R], readPreference: ReadPreference)(implicit writer: Writer[ResolvedCollectionCommand[C]], reader: Reader[R], ec: ExecutionContext): Future[ResponseResult[R]]

Attributes

Inherited from:
GenericCollectionWithCommands (hidden)
def runner: CommandWithPackRunner[P]

The command runner

The command runner

Attributes

Inherited from:
GenericCollectionWithCommands (hidden)
final def stats(scale: Int)(implicit ec: ExecutionContext): Future[CollectionStats]

Returns various information about this collection.

Returns various information about this collection.

import scala.concurrent.{ ExecutionContext, Future }

def getSize(coll: reactivemongo.api.bson.collection.BSONCollection)(
 implicit ec: ExecutionContext): Future[Double] =
 coll.stats(scale = 1024).map(_.size)

Attributes

scale

the scale factor (for example, to get all the sizes in kilobytes)

Inherited from:
CollectionMetaCommands (hidden)
final def stats()(implicit ec: ExecutionContext): Future[CollectionStats]

Returns various information about this collection.

Returns various information about this collection.

import scala.concurrent.{ ExecutionContext, Future }

def isCapped(coll: reactivemongo.api.bson.collection.BSONCollection)(
 implicit ec: ExecutionContext): Future[Boolean] =
 coll.stats().map(_.capped)

Attributes

Inherited from:
CollectionMetaCommands (hidden)
final def watch[T](offset: Option[Offset], pipeline: List[PipelineOperator], maxAwaitTime: Option[FiniteDuration], fullDocumentStrategy: Option[FullDocumentStrategy])(implicit reader: Reader[T]): WatchBuilder[T]

'''EXPERIMENTAL:''' Prepares a builder for watching the change stream of this collection.

'''EXPERIMENTAL:''' Prepares a builder for watching the change stream of this collection.

'''Note:''' The target mongo instance MUST be a replica-set (even in the case of a single node deployement).

import reactivemongo.api.Cursor
import reactivemongo.api.bson.BSONDocument
import reactivemongo.api.bson.collection.BSONCollection

def events(coll: BSONCollection): Cursor[BSONDocument] =
 coll.watch[BSONDocument]().cursor

Attributes

T

the type into which Change Events are deserialized

fullDocumentStrategy

if set to UpdateLookup, every update change event will be joined with the ''current'' version of the relevant document.

maxAwaitTimeMS

The maximum amount of time in milliseconds the server waits for new data changes before returning an empty batch. In practice, this parameter controls the duration of the long-polling behavior of the cursor.

offset

the change stream offset

pipeline

The sequence of aggregation stages to apply on events in the stream (see MongoDB documentation for a list of valid stages for a change stream).

reader

the reader of the resulting change events

Since:

MongoDB 3.6

Inherited from:
ChangeStreamOps

Concrete fields

lazy protected val version: MongoWireVersion

Upper MongoDB version (used for version checks)

Upper MongoDB version (used for version checks)

Attributes

Inherited fields

lazy protected val maxWireVersion: MongoWireVersion

Attributes

Inherited from:
UpdateOps
val pack: P

The serialization pack (BSON by default).

The serialization pack (BSON by default).

Used to resolve the types of values (by default BSONValue, BSONDocument, ...), and the related typeclasses (e.g. BSONDocumentReader ...).

Attributes

Inherited from:
PackSupport (hidden)

Implicits

Implicits

implicit lazy protected val unitReader: Reader[Unit]

Inherited implicits

final implicit lazy protected val resultReader: Reader[DefaultWriteResult]

Attributes

Inherited from:
CommandCodecsWithPack (hidden)
final implicit protected def updateReader: Reader[UpdateWriteResult]

Attributes

Inherited from:
UpdateCommand (hidden)