reactivemongo.api.collections.bson
(Since version 0.17.0) Internal: will be made private
Alias for type of the aggregation framework, depending on the type of the collection.
Alias for type of the aggregation framework, depending on the type of the collection.
reactivemongo.api.commands.AggregationFramework.PipelineOperator
Builder for delete operations.
Builder for delete operations.
Builder for insert operations.
Builder for insert operations.
Alias for reactivemongo.api.commands.AggregationFramework.PipelineOperator
Builder for update operations.
Builder for update operations.
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.
Aggregates the matching documents.
Aggregates the matching documents.
The type of the result elements. An implicit Reader[T]
typeclass for handling it has to be in the scope.
if true indicates to return the information on the processing of the pipeline
if true enables writing to temporary files
if true enables to bypass document validation during the operation
the read concern
the read preference for the result (default: primary)
the batch size (for the aggregation cursor; if None
use the default one)
the function to create the aggregation pipeline using the aggregation framework depending on the collection type
the result reader
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]( Group(BSONString(f"$$state"))( "totalPop" -> SumField("population")), List( Match(BSONDocument("totalPop" -> BSONDocument(f"$$gte" -> 10000000L)))) ).prepared.cursor.collect[List]( maxDocs = 3, err = Cursor.FailOnError[List[BSONDocument]]() ) }
The type of the result elements. An implicit Reader[T]
typeclass for handling it has to be in the scope.
the first aggregation operator of the pipeline
the sequence of MongoDB aggregation operations
if true indicates to return the information on the processing of the pipeline
if true enables writing to temporary files
if true enables to bypass document validation during the operation
the read concern
the read preference for the result
the writer concern to be used
the batch size (for the aggregation cursor; if None
use the default one)
the options for the result cursor
the time limit for processing operations on a cursor (maxTimeMS
)
the index to use (either the index name or the index document)
the comment to annotation the aggregation command
the collation
the result reader
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))
the size of the collection (number of bytes)
the maximum number of documents this capped collection can contain
Counts the matching documents.
Counts the matching documents.
the document selector
the maximum number of matching documents to count
the number of matching documents to skip before counting
the index to use (either the index name or the index document; see hint(..)
)
the read concern
the read preference for the result
Counts the matching documents.
Counts the matching documents.
the document selector
the maximum number of matching documents to count
the number of matching documents to skip before counting
the index to use (either the index name or the index document; see hint(..)
)
the read concern
Creates this collection.
Creates this collection.
import scala.concurrent.{ ExecutionContext, Future } import reactivemongo.api.CollectionMetaCommands def createIfNotExists(coll: CollectionMetaCommands)( implicit ec: ExecutionContext): Future[Unit] = coll.create(failsIfExists = true)
if true fails if the collection already exists (default: false)
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.CollectionMetaCommands import reactivemongo.api.commands.CommandError def createColl( coll: CollectionMetaCommands)(implicit ec: ExecutionContext) = coll.create().recover { case CommandError.Code(48) => // NamespaceExists println(s"Collection \${coll} already exists") }
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))
the size of the collection (number of bytes)
the maximum number of documents this capped collection can contain
If true should automatically add an index on the _id
field. By default, regular collections will have an indexed _id
field, in contrast to capped collections. This MongoDB option is deprecated and will be removed in a future release.
Creates a view on this collection, using an aggregation pipeline.
Creates a view on this collection, using an aggregation pipeline.
the name of the view to be created
the first (required) operator for the aggregation pipeline
the other pipeline operators
the view collation
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) val view: BSONCollection = coll.db("myview") }
The database which this collection belongs to.
The database which this collection belongs to.
Prepares a delete builder.
Prepares a delete builder.
the ordered behaviour
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)
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)
Returns the distinct values for a specified field across a single collection.
Returns the distinct values for a specified field across a single collection.
the element type of the distinct values
the container, that must be a scala.collection.Iterable
the field for which to return distinct values
the document selector, that specifies the documents from which to retrieve the distinct values.
the read concern
the collation
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)
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.CollectionMetaCommands def dropIfNotFound(coll: CollectionMetaCommands)( implicit ec: ExecutionContext): Future[Boolean] = coll.drop(failIfNotFound = true)
the flag to request whether it should fail
The default failover strategy for the methods of this collection.
The default failover strategy for the methods of this collection.
Finds the documents matching the given criteria (selector), with the projection applied.
Finds the documents matching the given criteria (selector), with the projection applied.
The type of the selector. An implicit Writer[S]
typeclass for handling it has to be in the scope.
the document selector
the projection document to select only a subset of each matching documents
the writer for the selector
the writer for the projection
A GenericQueryBuilder that you can use to to customize the query. You can obtain a cursor by calling the method reactivemongo.api.Cursor on this query builder.
Applies a findAndModify operation.
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]) }
the document selector
the modify operator to be applied
the document indicating the sort criteria (default: None
)
the projection fields
the writer concern to be used
the time limit for processing operations on a cursor (maxTimeMS
)
the collation
an array of filter documents that determines which array elements to modify for an update operation on an array field
the writer for the selector
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])
The type of the selector. An implicit Writer[S]
typeclass for handling it has to be in the scope.
the document selector
the document indicating the sort criteria
the projection fields
the writer concern to be used
the time limit for processing operations on a cursor (maxTimeMS
)
the collation
an array of filter documents that determines which array elements to modify for an update operation on an array field
the writer for the selector
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 }
the document selector
the update to be applied
the command result must be the new object instead of the old one.
if true, creates a new document if no document is matching, otherwise if at least one document matches, an update is applied
the document indicating the sort criteria (default: None
)
the projection fields
the writer concern to be used
the time limit for processing operations on a cursor (maxTimeMS
)
the collation
an array of filter documents that determines which array elements to modify for an update operation on an array field
the writer for the selector
the writer to create the document
Returns a hint for the given index specification
document.
Returns a hint for the given index specification
document.
the index specification document
Returns a hint for the given index name
.
Returns a hint for the given index name
.
the index name
Returns an index manager for this collection.
Returns an index manager for this collection.
import scala.concurrent.{ ExecutionContext, Future } import reactivemongo.api.CollectionMetaCommands def listIndexes(coll: CollectionMetaCommands)( implicit ec: ExecutionContext): Future[List[String]] = coll.indexesManager.list().map(_.flatMap { idx => idx.name.toList })
Returns a builder for insert operations.
Returns a builder for insert operations.
the ordered behaviour
the writer concern to be used
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)
Returns a builder for insert operations.
Returns a builder for insert operations.
the ordered behaviour
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)
Returns a builder for insert operations.
Returns a builder for insert operations. Uses the default write concern.
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)
Returns an unordered builder for insert operations.
Returns an unordered builder for insert operations. Uses the default write concern.
The name of the collection.
The name of the collection.
The default read concern
The default read concern
The default read preference
The default read preference
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)
the scale factor (for example, to get all the sizes in kilobytes)
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)
Returns an update builder.
Returns an update builder.
import scala.concurrent.ExecutionContext import reactivemongo.api.commands.WriteConcern import reactivemongo.api.bson.BSONDocument import reactivemongo.api.bson.collection.BSONCollection def withDefaultWriteConcern( coll: BSONCollection, query: BSONDocument, update: BSONDocument, wc: WriteConcern )(implicit ec: ExecutionContext) = coll.update( ordered = false, writeConcern = wc, bypassDocumentValidation = true ).one(query, update)
the ordered behaviour
the writer concern to be used
the flag to bypass document validation during the operation
Returns an update builder.
Returns an update builder.
import scala.concurrent.ExecutionContext import reactivemongo.api.commands.WriteConcern import reactivemongo.api.bson.BSONDocument import reactivemongo.api.bson.collection.BSONCollection def withDefaultWriteConcern( coll: BSONCollection, query: BSONDocument, update: BSONDocument, wc: WriteConcern )(implicit ec: ExecutionContext) = coll.update(ordered = false, writeConcern = wc).one(query, update)
the ordered behaviour
the writer concern to be used
Returns an update builder.
Returns an update builder.
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) }
the ordered behaviour
Returns an unordered update builder.
Returns an unordered update builder.
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) }
Returns an update modifier, to be used with findAndModify
.
Returns an update modifier, to be used with findAndModify
.
the update to be applied
the command result must be the new object instead of the old one.
if true, creates a new document if no document is matching, otherwise if at least one document matches, an update is applied
Upper MongoDB version (used for version checks)
Upper MongoDB version (used for version checks)
Prepares a builder for watching the change stream of this collection.
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 scala.concurrent.{ ExecutionContext, Future } import reactivemongo.api.Cursor import reactivemongo.api.bson.BSONDocument import reactivemongo.api.bson.collection.BSONCollection def events(coll: BSONCollection)( implicit ec: ExecutionContext): Cursor[BSONDocument] = coll.watch[BSONDocument]().cursor
the type into which Change Events are deserialized
The id of the last known Change Event, if any. The stream will resume just after that event.
The operation time before which all Change Events are known. Must be in the time range of the oplog. (since MongoDB 4.0)
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).
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.
if set to UpdateLookup, every update change event will be joined with the current version of the relevant document.
the reader of the resulting change events
MongoDB 3.6
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.
The default write concern
The default write concern
The read preference for the write operations (primary)
The read preference for the write operations (primary)
(Since version 0.19.0) Internal: will be made private
(Since version 0.16.0) Internal: will be made private
(Since version 0.16.0) Internal: will be made private
Aggregates the matching documents.
Aggregates the matching documents.
The type of the result elements. An implicit Reader[T]
typeclass for handling it has to be in the scope.
if true indicates to return the information on the processing of the pipeline
if true enables writing to temporary files
if true enables to bypass document validation during the operation
the read concern
the read preference for the result (default: primary)
the batch size (for the aggregation cursor; if None
use the default one)
the function to create the aggregation pipeline using the aggregation framework depending on the collection type
the result reader
the cursor flattener (by default use the builtin one)
(Since version 0.16.0) Use aggregateWith
(Since version 0.19.8) Use aggregator context with comment
(Since version 0.17.0) Use aggregator context with optional writeConcern
Gets another implementation of this collection.
Gets another implementation of this collection. An implicit CollectionProducer[C] must be present in the scope, or it will be the default implementation (reactivemongo.api.collections.bson.BSONCollection).
the failover strategy to override the default one
(Since version 0.13.0) Resolve the collection from DB
(Since version 0.12-RC2) No longer a ReactiveMongo case class
(Since version 0.12-RC2) No longer a ReactiveMongo case class
Counts the matching documents.
Counts the matching documents.
The type of hint. An implicit H => Hint
conversion has to be in the scope.
the document selector (default: None
to count all)
the maximum number of matching documents to count
the number of matching documents to skip before counting
the index to use (either the index name or the index document)
(Since version 0.16.0) Use count
with readConcern
parameter
(Since version 0.16.0) Use distinct
with Collation
Drops this collection.
Drops this collection.
The returned future will be completed with an error if this collection does not exist.
(Since version 0.12.0) Use drop(Boolean)
Finds the documents matching the given criteria (selector), with the projection applied.
Finds the documents matching the given criteria (selector), with the projection applied.
The type of the selector. An implicit Writer[S]
typeclass for handling it has to be in the scope.
the document selector
the projection document to select only a subset of each matching documents
the writer for the selector
the writer for the projection
A GenericQueryBuilder that you can use to to customize the query. You can obtain a cursor by calling the method reactivemongo.api.Cursor on this query builder.
(Since version 0.16.0) Use find
with optional projection
Finds the documents matching the given criteria (selector).
Finds the documents matching the given criteria (selector).
The type of the selector. An implicit Writer[S]
typeclass for handling it has to be in the scope.
the document selector
the writer for the selector
A GenericQueryBuilder that you can use to to customize the query. You can obtain a cursor by calling the method reactivemongo.api.Cursor on this query builder.
(Since version 0.16.0) Use find
with optional projection
(Since version 0.14.0) Use other findAndModify
(Since version 0.18.0) Use the other findAndRemove
(Since version 0.18.0) Use other findAndUpdate
Gets the full qualified name of this collection.
Gets the full qualified name of this collection.
(Since version 0.17.0) Internal: will be made private
Inserts a document into the collection and waits for the reactivemongo.api.commands.WriteResult.
Inserts a document into the collection and waits for the reactivemongo.api.commands.WriteResult.
The type of the document to insert. An implicit Writer[T]
typeclass for handling it has to be in the scope.
the document to insert
the writer concern to be used
the writer to create the document to be inserted
a future reactivemongo.api.commands.WriteResult that can be used to check whether the insertion was successful
import scala.concurrent.ExecutionContext import reactivemongo.api.bson.BSONDocument import reactivemongo.api.bson.collection.BSONCollection def withDefaultWriteConcern(coll: BSONCollection, myDoc: BSONDocument)( implicit ec: ExecutionContext) = coll.insert(myDoc)
(Since version 0.16.1) Use .insert(ordered = false).one(..)
(Since version 0.12-RC2) No longer a ReactiveMongo case class
(Since version 0.12-RC2) No longer a ReactiveMongo case class
The type of the selector. An implicit Writer[S]
typeclass for handling it has to be in the scope.
the writer concern to be used
the writer for the selector
a future reactivemongo.api.commands.WriteResult that can be used to check whether the removal was successful
(Since version 0.13.1) Use delete().one(selector, limit)
Returns a removal modifier, to be used with findAndModify
.
Returns a removal modifier, to be used with findAndModify
.
(Since version 0.16.0) Internal: will be made private
Renames this collection.
Renames this collection.
the new name of this collection
if a collection of name to
already exists, then drops that collection before renaming this one
A failure if the dropExisting option is false and the target collection already exists.
(Since version 0.12.4) Use reactivemongo.api.DBMetaCommands.renameCollection on the admin database instead.
Gets another collection in the current database.
Gets another collection in the current database. An implicit CollectionProducer[C] must be present in the scope, or it will be the default implementation (reactivemongo.api.collections.bson.BSONCollection).
the name of another collection
the failover strategy to override the default one
(Since version 0.19.4) Use DB.collection(name)
Updates one or more documents matching the given selector with the given modifier or update object.
Updates one or more documents matching the given selector with the given modifier or update object.
The type of the selector. An implicit Writer[S]
typeclass for handling it has to be in the scope.
The type of the modifier or update object. An implicit Writer[T]
typeclass for handling it has to be in the scope.
the selector object, for finding the documents to update.
the modifier object (with special keys like $set) or replacement object.
the writer concern to be used
if true, creates a new document if no document is matching, otherwise if at least one document matches, an update is applied (defaults: false
)
states whether the update may be done on all the matching documents (default: false
)
the writer for the selector
the writer to create the document
a future reactivemongo.api.commands.WriteResult that can be used to check whether the insertion was successful
(Since version 0.16.1) Use .update(ordered = false).one(..)
(Since version 0.19.0) Use
reactivemongo.api.bson.collection.BSONCollection