Operations: What are the Operations

The RavenDB client API is built with the notion of layers. At the top, and what you will usually interact with, are the DocumentStore and the DocumentSession.

They in turn, are built on top of the notion of Operations and Commands.

Operations are an encapsulation of a set of low level commands which are used to manipulate data, execute administrative tasks, and change the configuration on a server.

They are available in the DocumentStore under the Operations, Maintenance, and Maintenance.Server properties.

Common Operations

Common operations include set based operations for Patching or removal of documents by using queries (more can be read here).
There is also the ability to handle distributed Compare Exchange operations and manage Attachments.

How to Send an Operation

In order to excecute an operation, you will need to use the Send or SendAsync methods. Avaliable overloads are:

void Send(IOperation operation, SessionInfo sessionInfo = null);

TResult Send<TResult>(IOperation<TResult> operation, SessionInfo sessionInfo = null);

Operation Send(IOperation<OperationIdResult> operation, SessionInfo sessionInfo = null);

PatchStatus Send(PatchOperation operation);

PatchOperation.Result<TEntity> Send<TEntity>(PatchOperation<TEntity> operation);
Task SendAsync(IOperation operation, CancellationToken token = default(CancellationToken), SessionInfo sessionInfo = null);

Task<TResult> SendAsync<TResult>(IOperation<TResult> operation, CancellationToken token = default(CancellationToken), SessionInfo sessionInfo = null);

Task<Operation> SendAsync(IOperation<OperationIdResult> operation, CancellationToken token = default(CancellationToken), SessionInfo sessionInfo = null);

Task<PatchStatus> SendAsync(PatchOperation operation, CancellationToken token = default(CancellationToken));

Task<PatchOperation.Result<TEntity>> SendAsync<TEntity>(PatchOperation<TEntity> operation, CancellationToken token = default(CancellationToken));

The following operations are available:

Compare Exchange

Attachments

Patching

Misc

Example - Get Attachment

using (AttachmentResult fetchedAttachment = documentStore.Operations.Send(new GetAttachmentOperation("users/1", "file.txt", AttachmentType.Document, null)))
{
    //do stuff with the attachment stream --> fetchedAttachment.Stream                                       
}
using (AttachmentResult fetchedAttachment = await documentStore.Operations.SendAsync(new GetAttachmentOperation("users/1", "file.txt", AttachmentType.Document, null)))
{
    //do stuff with the attachment stream --> fetchedAttachment.Stream                                       
}

Maintenance Operations

Maintenance operations include operations for changing the configuration at runtime and for management of index operations.

How to Send an Operation

void Send(IMaintenanceOperation operation);

TResult Send<TResult>(IMaintenanceOperation<TResult> operation);

Operation Send(IMaintenanceOperation<OperationIdResult> operation);
Task SendAsync(IMaintenanceOperation operation, CancellationToken token = default(CancellationToken));

Task<TResult> SendAsync<TResult>(IMaintenanceOperation<TResult> operation, CancellationToken token = default(CancellationToken));

Task<Operation> SendAsync(IMaintenanceOperation<OperationIdResult> operation, CancellationToken token = default(CancellationToken));

The following maintenance operations are available:

Client Configuration

ETL

Indexes

Misc

Example - Stop Index

documentStore.Maintenance.Send(new StopIndexOperation("Orders/ByCompany"));
await documentStore.Maintenance.SendAsync(new StopIndexOperation("Orders/ByCompany"));

Server Operations

These type of operations contain various administrative and miscellaneous configuration operations.

How to Send an Operation

void Send(IServerOperation operation);

TResult Send<TResult>(IServerOperation<TResult> operation);

Operation Send(IServerOperation<OperationIdResult> operation);
Task SendAsync(IServerOperation operation, CancellationToken token = default(CancellationToken));

Task<TResult> SendAsync<TResult>(IServerOperation<TResult> operation, CancellationToken token = default(CancellationToken));

Task<Operation> SendAsync(IServerOperation<OperationIdResult> operation, CancellationToken token = default(CancellationToken));

The following server-wide operations are available:

Client Certificates

Server-wide Configuration

Cluster Management

Miscellaneous

Example - Get Build Number

var getBuildNumberResult = documentStore.Maintenance.Server.Send(new GetBuildNumberOperation());
Console.WriteLine(getBuildNumberResult.BuildVersion);
var buildNumber = await documentStore.Maintenance.Server.SendAsync(new GetBuildNumberOperation());
Console.WriteLine(buildNumber.BuildVersion);

Remarks

Note

By default, operations available in store.Operations or store.Maintenance are working on a default database that was setup for that store. To switch operations to a different database that is available on that server use the ForDatabase method.