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 Objecttrait Matchableclass Any
Members list
Type members
Inherited classlikes
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 CommandWithResult[Unit]trait CommandWithPack[P]trait Commandclass Objecttrait Matchableclass 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)
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)
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
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)
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)
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
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
'''EXPERIMENTAL:''' API may change without notice.
'''EXPERIMENTAL:''' API may change without notice.
Attributes
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
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
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
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
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
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
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.
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.
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.
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)
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)
Attributes
- Inherited from:
- CreateUserCommand (hidden)
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)
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)
The GridFS with the default serialization.
The GridFS with the default serialization.
Attributes
- prefix
the collection prefix
- Inherited from:
- DBMetaCommands (hidden)
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)
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)
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)
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)