forked from ReactiveMongo/ReactiveMongo
-
Notifications
You must be signed in to change notification settings - Fork 1
Commands
Stephane Godbillon edited this page Jul 22, 2012
·
1 revision
A MongoDB Command is a basically a query that is run on the db.$cmd
collection and gives back one document as a result.
The org.asyncmongo.api.Collection
class defines a command
method:
def command(command: Command) :Future[command.Result]
This method takes a command and returns a future result of the dependent type command.Result
. All the predefined commands are in the org.asyncmongo.protocol.commands
package.
Let's examine a concrete example with the FindAndModify command:
val command = FindAndModify(
collection.collectionName,
selector,
Update(modifier, false))
val result :Future[Option[BSONDocument]] = collection.command(command)
result.onComplete {
case Left(error) => throw error
case Right(maybeDocument) => println(maybeDocument)
}
A command should extend the trait Command
, set the dependent type Result
, define the ResultMaker
value (a value that produces an instance of Result
from a given Response
), and implement the abstract method def makeDocuments :Bson
(the documents that are the operands of the command).
/**
* A MongoDB Command.
*
* Basically, it's as query that is performed on any db.\$cmd collection
* and gives back one document as a result.
*/
trait Command {
/**
* This command's result type.
*/
type Result
/**
* Deserializer for this command's result.
*/
val ResultMaker :CommandResultMaker[Result]
/**
* States if this command can be run on secondaries.
*/
val slaveOk :Boolean = false
/**
* Makes the [[org.asyncmongo.bson.Bson]] for documents that will be send as body of this command's query.
*/
def makeDocuments :Bson
/**
* Produces a [[org.asyncmongo.protocol.commands.MakableCommand]] instance of this command.
*
* @param db name of the target database.
*/
def apply(db: String) :MakableCommand = new MakableCommand(db, this)
}
/**
* Handler for deserializing commands results.
*
* @tparam Result the result type of this command.
*/
trait CommandResultMaker[Result] {
/**
* Deserializes the given response into an instance of Result.
*/
def apply(response: Response) :Result
}