Commands Overview
-
RavenDB's Client API is structured in layers.
At the highest layer, you interact with the document store and the document session,
which handle most common database tasks like loading, saving, and querying documents. -
Beneath this high-level interface are Operations and Commands:
-
Operations:
-
Operations provide management functionality outside the session's context,
like creating a database, performing bulk actions, or managing server-wide configurations. -
Learn more about Operations in what are Operations.
-
-
Commands:
-
All high-level methods and Operations are built on top of Commands.
Commands form the lowest-level operations that directly communicate with the server. -
For example, a session’s Load method translates internally to a LoadOperation,
which ultimately relies on a GetDocumentsCommand to fetch data from the server. -
Commands are responsible for sending the appropriate request to the server using a
Request Executor
,
and parsing the server's response. -
All commands can be executed using either the Store's Request Executor or the Session's Request Executor,
regardless of whether the command is session-related or not.
-
-
-
This layered structure lets you work at any level, depending on your needs.
-
In this page:
Execute command - using the Store Request Executor
This example shows how to execute the low-level CreateSubscriptionCommand
via the Store.
(For examples of creating a subscription using higher-level methods, see subscription creation examples).
// Using the store object
using (var store = new DocumentStore())
// Allocate a context from the store's context pool for executing the command
using (store.GetRequestExecutor().ContextPool.AllocateOperationContext(out var context))
{
// Define a command
var cmd = new CreateSubscriptionCommand(store.Conventions,
new SubscriptionCreationOptions()
{
Name = "Orders subscription",
Query = "from Orders"
});
// Call 'Execute' on the store's Request Executor to send the command to the server,
// pass the command and the store context.
store.GetRequestExecutor().Execute(cmd, context);
}
// Using the store object
using (var store = new DocumentStore())
// Allocate a context from the store's context pool for executing the command
using (store.GetRequestExecutor().ContextPool.AllocateOperationContext(out var context))
{
// Define a command
var cmd = new CreateSubscriptionCommand(store.Conventions,
new SubscriptionCreationOptions()
{
Name = "Orders subscription",
Query = "from Orders"
});
// Call 'ExecuteAsync' on the store's Request Executor to send the command to the server,
// pass the command and the store context.
await store.GetRequestExecutor().ExecuteAsync(cmd, context);
}
Execute command - using the Session Request Executor
This example shows how to execute the low-level GetDocumentsCommand
via the Session.
(For loading a document using higher-level methods, see loading entities).
// Using the session
using (var session = store.OpenSession())
{
// Define a command
var cmd = new GetDocumentsCommand(store.Conventions, "orders/1-A", null, false);
// Call 'Execute' on the session's Request Executor to send the command to the server
// Pass the command and the 'Session.Advanced.Context'
session.Advanced.RequestExecutor.Execute(cmd, session.Advanced.Context);
// Access the results
var blittable = (BlittableJsonReaderObject)cmd.Result.Results[0];
// Deserialize the blittable JSON into your typed object
var order = session.Advanced.JsonConverter.FromBlittable<Order>(ref blittable,
"orders/1-A", false);
// Now you have a strongly-typed Order object that can be accessed
var orderedAt = order.OrderedAt;
}
// Using the session
using (var asyncSession = store.OpenAsyncSession())
{
// Define a command
var cmd = new GetDocumentsCommand(store.Conventions, "orders/1-A", null, false);
// Call 'ExecuteAsync' on the session's Request Executor to send the command to the server
// Pass the command and the 'Session.Advanced.Context'
await asyncSession.Advanced.RequestExecutor.ExecuteAsync(cmd,
asyncSession.Advanced.Context);
// Access the results
var blittable = (BlittableJsonReaderObject)cmd.Result.Results[0];
// Deserialize the blittable JSON into your typed object
var order = asyncSession.Advanced.JsonConverter.FromBlittable<Order>(ref blittable,
"orders/1-A", true);
// Now you have a strongly-typed Order object that can be accessed
var orderedAt = order.OrderedAt;
}
-
Note that the transaction created for the HTTP request when executing the command
is separate from the transaction initiated by the session's SaveChanges method,
even if both are called within the same code block. -
Learn more about transactions in RavenDB in Transaction support.
Available commands
-
The following low-level commands, which inherit from
RavenCommand
, are available:- ConditionalGetDocumentsCommand
- CreateSubscriptionCommand
- DeleteDocumentCommand
- DeleteSubscriptionCommand
- DropSubscriptionConnectionCommand
- ExplainQueryCommand
- GetClusterTopologyCommand
- GetConflictsCommand
- GetDatabaseTopologyCommand
- GetDocumentsCommand
- GetIdentitiesCommand
- GetNextOperationIdCommand
- GetNodeInfoCommand
- GetOperationStateCommand
- GetRawStreamResultCommand
- GetRevisionsBinEntryCommand
- GetRevisionsCommand
- GetSubscriptionsCommand
- GetSubscriptionStateCommand
- GetTcpInfoCommand
- GetTrafficWatchConfigurationCommand
- HeadAttachmentCommand
- HeadDocumentCommand
- HiLoReturnCommand
- IsDatabaseLoadedCommand
- KillOperationCommand
- MultiGetCommand
- NextHiLoCommand
- NextIdentityForCommand
- PutDocumentCommand
- PutSecretKeyCommand
- QueryCommand
- QueryStreamCommand
- SeedIdentityForCommand
- SingleNodeBatchCommand
- WaitForRaftIndexCommand
Syntax
void Execute<TResult>(RavenCommand<TResult> command,
JsonOperationContext context,
SessionInfo sessionInfo = null);
Task ExecuteAsync<TResult>(RavenCommand<TResult> command,
JsonOperationContext context,
SessionInfo sessionInfo = null,
CancellationToken token = default(CancellationToken));