DB

reactivemongo.api.DB
final class DB

The reference to a MongoDB database, obtained from a reactivemongo.api.MongoConnection.

import scala.concurrent.ExecutionContext
import reactivemongo.api.MongoConnection

def foo(connection: MongoConnection)(implicit ec: ExecutionContext) = {
 val db = connection.database("plugin")
 val _ = db.map(_("acoll")) // Collection reference
}

Attributes

connection

the reactivemongo.api.MongoConnection that will be used to query this database

failoverStrategy

the default failover strategy for sending requests

name

this database name

Graph
Supertypes
class Object
trait Matchable
class Any

Members list

Concise view

Type members

Inherited classlikes

final protected class CreateUser(val name: String, val pwd: Option[String], val customData: Option[Document], val roles: List[UserRole], val digestPassword: Boolean, val writeConcern: Option[WriteConcern], val authenticationRestrictions: List[AuthenticationRestriction], val mechanisms: List[AuthenticationMode]) extends Command with CommandWithPack[P] with CommandWithResult[Unit]

The createUser command.

The createUser command.

Attributes

customData

the custom data to associate with the user account

digestPassword

when true, the mongod instance will create the hash of the user password (default: true)

name

the name of the user to be created

pwd

the user password (not required if the database uses external credentials)

roles

the roles granted to the user, possibly an empty to create users without roles

writeConcern

the optional level of write concern

Inherited from:
CreateUserCommand (hidden)
Graph
Supertypes
trait Command
class Object
trait Matchable
class Any

Value members

Concrete methods

Aborts the transaction associated with the current client session (since MongoDB 4.0), if any otherwise does nothing .

Aborts the transaction associated with the current client session (since MongoDB 4.0), if any otherwise does nothing .

import scala.concurrent.ExecutionContext
import reactivemongo.api.DB

def equivalentTo(db: DB)(implicit ec: ExecutionContext) =
 db.abortTransaction(failIfNotStarted = false)

Attributes

Returns:

The database reference with transaction aborted (but not session)

def abortTransaction(failIfNotStarted: Boolean)(implicit ec: ExecutionContext): Future[DB]

Aborts the transaction associated with the current client session (since MongoDB 4.0), if any otherwise does nothing .

Aborts the transaction associated with the current client session (since MongoDB 4.0), if any otherwise does nothing .

Attributes

Returns:

The database reference with transaction aborted (but not session)

def apply[C <: Collection](name: String, failoverStrategy: FailoverStrategy)(implicit producer: CollectionProducer[C]): C

Returns a reactivemongo.api.Collection reference from this database (alias for the collection method).

Returns a reactivemongo.api.Collection reference from this database (alias for the collection method).

Attributes

C

the Collection type

failoverStrategy

the failover strategy to override the default one

name

the name of the collection to resolve

Authenticates the connection on this database.

Authenticates the connection on this database.

Attributes

password

the user password

user

the name of the user

See also:

MongoConnection.authenticate

def collection[C <: Collection](name: String, failoverStrategy: FailoverStrategy)(implicit producer: CollectionProducer[C]): C

Returns a reactivemongo.api.Collection reference from this database.

Returns a reactivemongo.api.Collection reference from this database.

Attributes

C

the Collection type

failoverStrategy

the failover strategy to override the default one

import reactivemongo.api.DB
def resolveColl(db: DB) = db.collection("acoll")
name

the name of the collection to resolve

Commits the transaction associated with the current client session (since MongoDB 4.0), if any otherwise does nothing .

Commits the transaction associated with the current client session (since MongoDB 4.0), if any otherwise does nothing .

import scala.concurrent.ExecutionContext
import reactivemongo.api.DB

def equivalentTo(db: DB)(implicit ec: ExecutionContext) =
 db.commitTransaction(failIfNotStarted = false)

Attributes

Returns:

The database reference with transaction commited (but not session)

def commitTransaction(failIfNotStarted: Boolean)(implicit ec: ExecutionContext): Future[DB]

Commits the transaction associated with the current client session (since MongoDB 4.0), if any otherwise does nothing .

Commits the transaction associated with the current client session (since MongoDB 4.0), if any otherwise does nothing .

import scala.concurrent.ExecutionContext
import reactivemongo.api.DB

def commitIt(db: DB)(implicit ec: ExecutionContext) =
 db.commitTransaction(failIfNotStarted = true)

Attributes

Returns:

The database reference with transaction commited (but not session)

def endSession()(implicit ec: ExecutionContext): Future[DB]

Ends (closes) the session associated with this database reference (since MongoDB 3.6), if any otherwise does nothing .

Ends (closes) the session associated with this database reference (since MongoDB 3.6), if any otherwise does nothing .

import scala.concurrent.ExecutionContext
import reactivemongo.api.DB

def equivalentTo(db: DB)(implicit ec: ExecutionContext) =
 db.endSession(failIfNotStarted = false)

Attributes

Returns:

The database reference with session ended

def endSession(failIfNotStarted: Boolean)(implicit ec: ExecutionContext): Future[DB]

Ends (closes) the session associated with this database reference (since MongoDB 3.6), if any otherwise does nothing .

Ends (closes) the session associated with this database reference (since MongoDB 3.6), if any otherwise does nothing .

Attributes

Returns:

The database reference with session ended

def getMore[P <: SerializationPack, T](pack: P, reference: Reference, readPreference: ReadPreference, failoverStrategy: FailoverStrategy, maxTimeMS: Option[Long])(implicit reader: Reader[T], cp: CursorProducer[T]): ProducedCursor

'''EXPERIMENTAL:''' API may change without notice.

'''EXPERIMENTAL:''' API may change without notice.

Attributes

def killSession()(implicit ec: ExecutionContext): Future[DB]

Kills (aborts) the session associated with this database reference (since MongoDB 3.6), if any otherwise does nothing .

Kills (aborts) the session associated with this database reference (since MongoDB 3.6), if any otherwise does nothing .

import scala.concurrent.ExecutionContext
import reactivemongo.api.DB

def equivalentTo(db: DB)(implicit ec: ExecutionContext) =
 db.killSession(failIfNotStarted = false)

Attributes

Returns:

The database reference with session aborted

def killSession(failIfNotStarted: Boolean)(implicit ec: ExecutionContext): Future[DB]

Kills (aborts) the session associated with this database reference (since MongoDB 3.6), if any otherwise does nothing .

Kills (aborts) the session associated with this database reference (since MongoDB 3.6), if any otherwise does nothing .

Attributes

Returns:

The database reference with session aborted

def runCommand[R, C <: Command & CommandWithResult[R]](command: C & CommandWithResult[R], failoverStrategy: FailoverStrategy, readPreference: ReadPreference)(implicit writer: BSONDocumentWriter[C], reader: BSONDocumentReader[R], ec: ExecutionContext): Future[R]

Attributes

C

the reactivemongo.api.commands.Command type

R

the result type

command

the command to be executed on the current database

failoverStrategy

the failover strategy to override the default one

readPreference

the read preference for the result

reader

the reader for the result of command execution

writer

the writer for the command

Returns:

A single result from command execution

def runCommand[C <: Command](command: C, failoverStrategy: FailoverStrategy)(implicit writer: BSONDocumentWriter[C]): CursorFetcher[Pack, Cursor]

Attributes

C

the reactivemongo.api.commands.Command type

command

the command to be executed on the current database

failoverStrategy

the failover strategy to override the default one

reader

the reader for the result of command execution

writer

the writer for the command

Returns:

A cursor for the command results

def runCommand(command: Document, failoverStrategy: FailoverStrategy): CursorFetcher[Pack, Cursor]

Run a raw command (represented by a document).

Run a raw command (represented by a document).

import reactivemongo.api.FailoverStrategy
import reactivemongo.api.bson.BSONDocument

def getUsers(db: reactivemongo.api.DB) =
 db.runCommand(BSONDocument("usersInfo" -> 1), FailoverStrategy.default)

Attributes

command

the command to be executed on the current database

failoverStrategy

the failover strategy to override the default one

readPreference

the read preference for the result

Returns:

A cursor for the command results

def startSession()(implicit ec: ExecutionContext): Future[DB]

Starts a new session (since MongoDB 3.6), does nothing if a session has already being started .

Starts a new session (since MongoDB 3.6), does nothing if a session has already being started .

import scala.concurrent.ExecutionContext
import reactivemongo.api.DB

def equivalentTo(db: DB)(implicit ec: ExecutionContext) =
 db.startSession(failIfAlreadyStarted = false)

Attributes

Returns:

The database reference updated with a new session

def startSession(failIfAlreadyStarted: Boolean)(implicit ec: ExecutionContext): Future[DB]

Starts a new session (since MongoDB 3.6).

Starts a new session (since MongoDB 3.6).

import scala.concurrent.ExecutionContext
import reactivemongo.api.DB

def startIt(db: DB)(implicit ec: ExecutionContext) =
 db.startSession(failIfAlreadyStarted = true)

Attributes

failIfAlreadyStarted

if true fails if a session is already started

Returns:

The database reference updated with a new session, if none is already started with the current reference.

def startTransaction(writeConcern: Option[WriteConcern])(implicit ec: ExecutionContext): Future[DB]

Starts a transaction (since MongoDB 4.0), if none is already started with the current client session otherwise does nothing.

Starts a transaction (since MongoDB 4.0), if none is already started with the current client session otherwise does nothing.

It fails if no session is previously started (see startSession).

import scala.concurrent.ExecutionContext
import reactivemongo.api.{ DB, WriteConcern }

def equivalentTo(db: DB, aWriteConcern: Option[WriteConcern])(
 implicit ec: ExecutionContext) =
 db.startTransaction(aWriteConcern, failIfAlreadyStarted = false)

Attributes

writeConcern

the write concern for the transaction operation

Returns:

The database reference with transaction.

def startTransaction(writeConcern: Option[WriteConcern], failIfAlreadyStarted: Boolean)(implicit ec: ExecutionContext): Future[DB]

Starts a transaction (since MongoDB 4.0), if none is already started with the current client session otherwise does nothing.

Starts a transaction (since MongoDB 4.0), if none is already started with the current client session otherwise does nothing.

It fails if no session is previously started (see startSession).

import scala.concurrent.ExecutionContext
import reactivemongo.api.{ DB, WriteConcern }

def doIt(db: DB, aWriteConcern: Option[WriteConcern])(
 implicit ec: ExecutionContext) =
 db.startTransaction(aWriteConcern, failIfAlreadyStarted = true)

Attributes

writeConcern

the write concern for the transaction operation

Returns:

The database reference with transaction.

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

Inherited methods

Returns the names of the collections in this database.

Returns the names of the collections in this database.

import scala.concurrent.{ ExecutionContext, Future }
import reactivemongo.api.DB

def listCollections(db: DB)(
 implicit ec: ExecutionContext): Future[List[String]] =
 db.collectionNames

Attributes

Inherited from:
DBMetaCommands (hidden)
final def createUser[T](user: String, pwd: Option[String], customData: Option[T], roles: List[UserRole], digestPassword: Boolean, writeConcern: WriteConcern, restrictions: List[AuthenticationRestriction], mechanisms: List[AuthenticationMode])(implicit ec: ExecutionContext, w: BSONDocumentWriter[T]): Future[Unit]

Create the user with given properties.

Create the user with given properties.

import scala.concurrent.{ ExecutionContext, Future }

import reactivemongo.api.{
 DB,
 ScramSha256Authentication,
 ScramSha1Authentication,
 WriteConcern
}
import reactivemongo.api.commands.UserRole

def createReadWriteUser(db: DB, name: String)(
 implicit ec: ExecutionContext): Future[Unit] =
 db.createUser(
   user = name,
   pwd = None, // no initial password
   roles = List(UserRole("readWrite")),
   digestPassword = true,
   writeConcern = WriteConcern.Default,
   restrictions = List.empty,
   mechanisms = List(
     ScramSha1Authentication, ScramSha256Authentication))

Attributes

T

the type of custom data associated with the created user

customData

the custom data to associate with the user account

digestPassword

when true, the mongod instance will create the hash of the user password (default: true)

mechanisms

the authentication mechanisms (e.g. ScramSha1Authentication)

pwd

the user password (not required if the database uses external credentials)

restrictions

the authentication restriction

roles

the roles granted to the user, possibly an empty to create users without roles

user

the name of the user to be created

writeConcern

the optional level of write concern

Inherited from:
DBMetaCommands (hidden)
final protected def createUserWriter(version: MongoWireVersion): Writer[CreateUser]

Attributes

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

Drops this database.

Drops this database.

import scala.concurrent.{ ExecutionContext, Future }
import reactivemongo.api.DB

def dropDB(db: DB)(
 implicit ec: ExecutionContext): Future[Unit] = db.drop()

Attributes

Inherited from:
DBMetaCommands (hidden)
final def gridfs[P <: SerializationPack](pack: P, prefix: String): GridFS[P]

The GridFS with the default serialization.

The GridFS with the default serialization.

Attributes

P

the type of serialization

pack

the serialization pack

prefix

the collection prefix

Inherited from:
DBMetaCommands (hidden)
final def gridfs(prefix: String): GridFS[Pack]

The GridFS with the default serialization.

The GridFS with the default serialization.

Attributes

prefix

the collection prefix

Inherited from:
DBMetaCommands (hidden)
final def gridfs: GridFS[Pack]

The GridFS with the default serialization and collection prefix.

The GridFS with the default serialization and collection prefix.

import reactivemongo.api.DB
import reactivemongo.api.bson.BSONDocument

def findFile(db: DB, query: BSONDocument) = db.gridfs.find(query)

Attributes

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

Returns an index manager for this database.

Returns an index manager for this database.

import scala.concurrent.{ ExecutionContext, Future }

import reactivemongo.api.DB
import reactivemongo.api.indexes.NSIndex

def listIndexes(db: DB)(
 implicit ec: ExecutionContext): Future[List[String]] =
 db.indexesManager.list().map(_.flatMap { (ni: NSIndex) =>
   ni.index.name.toList
 })

Attributes

Inherited from:
DBMetaCommands (hidden)
final def ping(readPreference: ReadPreference)(implicit ec: ExecutionContext): Future[Boolean]

Tests if the server, resolved according to the given read preference, responds to commands.

Tests if the server, resolved according to the given read preference, responds to commands.

Attributes

Returns:

true if successful (even if the server is write locked)

Since:

MongoDB 3.0

import scala.concurrent.{ ExecutionContext, Future }
import reactivemongo.api.DB
def pingDB(db: DB)(
 implicit ec: ExecutionContext): Future[Boolean] =
 db.ping() // with default ReadPreference
Inherited from:
DBMetaCommands (hidden)
final def renameCollection[C <: Collection](db: String, from: String, to: String, dropExisting: Boolean, failoverStrategy: FailoverStrategy)(implicit ec: ExecutionContext, producer: CollectionProducer[C]): Future[C]

Renames a collection. Can only be executed if the this database reference is the admin one.

Renames a collection. Can only be executed if the this database reference is the admin one.

import scala.concurrent.{ ExecutionContext, Future }
import reactivemongo.api.DB

def addCollSuffix(
 admin: DB,
 coll: String,
 suffix: String)(implicit ec: ExecutionContext): Future[Unit] =
 admin.renameCollection("myDB", coll, coll + suffix).map(_ => {})

Attributes

db

the name of the database where the collection exists with the current name

dropExisting

If a collection of name to already exists, then drops that collection before renaming this one.

from

the current name of the collection, in the specified db

to

the new name of this collection (inside the same db)

Returns:

a failure if the dropExisting option is false and the target collection already exists

Inherited from:
DBMetaCommands (hidden)

Concrete fields

val pack: Pack

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