Revert Document to Revision Operation
-
This article describes how to revert specific documents to specific revisions using the
RevertRevisionsByIdOperation
operation. -
To revert documents from all collections (or from selected collections) to a specified point in time,
see Revert documents to revisions. -
By default, the operation will be applied to the default database.
To operate on a different database see switch operations to different database. -
In this page:
Overview
-
To revert a document to a specific revision, provide the document ID and the change-vector of the target revision to the
RevertRevisionsByIdOperation
operation.
The document content will be overwritten by the content of the specified revision. -
An exception will be thrown if the revision's change-vector is not found, does not exist for the specified document, or belongs to a different document.
-
Reverting a document with this operation can be executed even if the revisions configuration is disabled:
- When revisions are enabled:
Reverting the document creates a new revision containing the content of the target revision. - When revisions are disabled:
The document is reverted to the target revision without creating a new revision.
- When revisions are enabled:
-
In addition to the document itself, reverting will impact Document Extensions as follows:
- Attachments:
If the target revision owns attachments, they are restored to their state when the revision was created. - Counters:
If the target revision owns counters, they are restored to functionality with their values at the time the revision was created. - Time series:
Time series data is Not reverted. Learn more here.
- Attachments:
-
When executing this operation on a document that had revisions and was deleted, placing it in the Revisions Bin, the document will be recreated with the content of the specified target revision and will be removed from the Revisions Bin.
How to obtain a revision's change-vector:
The change-vector of a revision can be obtained via:
- The Client API - follow the code in the examples below
- Or from the document view in the Studio
Get the revision's change-vector
- Go to the Revisions tab in the document view.
- Click a revision to view
- The document view will display the content of the revision.
This top label indicates that you are viewing a revision and not the current document. - Click the copy button in the Properties pane to copy this revision's change-vector to your clipboard.
Revert single document
Using RavenDB's sample data, document orders/1-A has a total of 7 revisions.
In this example, we revert document orders/1-A to its very first revision.
using (var session = store.OpenSession())
{
// Get the revisions metadata for the document you wish to revert
// ==============================================================
var revisionsMetadata = session.Advanced.Revisions
.GetMetadataFor(id: "orders/1-A");
// Get the CV of the revision you wish to revert to:
// =================================================
// Note: revisionsMetadata[0] is the latest revision,
// so specify the index of the revision you want.
// In this example, it will be the very first revision of the document:
var numberOfRevisions = revisionsMetadata.Count();
var changeVector = revisionsMetadata[numberOfRevisions-1]
.GetString(Constants.Documents.Metadata.ChangeVector);
// Execute the operation
store.Operations.Send(
// Pass the document ID and the change-vector of the revision to revert to
new RevertRevisionsByIdOperation("orders/1-A", changeVector));
}
using (var asyncSession = store.OpenAsyncSession())
{
// Get the revisions metadata for the document you wish to revert
// ==============================================================
var revisionsMetadata = await asyncSession.Advanced.Revisions
.GetMetadataForAsync(id: "Orders/1-A");
// Get the CV of the revision you wish to revert to:
// =================================================
// Note: revisionsMetadata[0] is the latest revision,
// so specify the index of the revision you want.
// In this example, it will be the very first revision of the document:
var numberOfRevisions = revisionsMetadata.Count();
var changeVector = revisionsMetadata[numberOfRevisions-1]
.GetString(Constants.Documents.Metadata.ChangeVector);
// Execute the operation
await store.Operations.SendAsync(
// Pass the document ID and the change-vector of the revision to revert to
new RevertRevisionsByIdOperation("Orders/1-A", changeVector));
}
Revert multiple documents
You can use the operation to revert multiple documents.
Note: The documents do not need to belong to the same collection.
using (var session = store.OpenSession())
{
// Get the revisions metadata for the documents you wish to revert
var revisionsMetadata1 = session.Advanced.Revisions
.GetMetadataFor(id: "orders/1-A");
var revisionsMetadata2 = session.Advanced.Revisions
.GetMetadataFor(id: "users/999");
// Get the CV of the revisions you wish to revert to
var changeVector1 = revisionsMetadata1[2]
.GetString(Constants.Documents.Metadata.ChangeVector);
var changeVector2 = revisionsMetadata1[3]
.GetString(Constants.Documents.Metadata.ChangeVector);
// Execute the operation
store.Operations.Send(
// Pass the document IDs and the change-vector of the revisions to revert to
new RevertRevisionsByIdOperation(new Dictionary<string, string>()
{ { "orders/1-A", changeVector1 }, { "users/999", changeVector2 } }));
}
using (var asyncSession = store.OpenAsyncSession())
{
// Get the revisions metadata for the documents you wish to revert
var revisionsMetadata1 = await asyncSession.Advanced.Revisions
.GetMetadataForAsync(id: "orders/1-A");
var revisionsMetadata2 = await asyncSession.Advanced.Revisions
.GetMetadataForAsync(id: "users/999");
// Get the CV of the revisions you wish to revert to
var changeVector1 = revisionsMetadata1[2]
.GetString(Constants.Documents.Metadata.ChangeVector);
var changeVector2 = revisionsMetadata1[3]
.GetString(Constants.Documents.Metadata.ChangeVector);
// Execute the operation
await store.Operations.SendAsync(
// Pass the document IDs and the change-vector of the revisions to revert to
new RevertRevisionsByIdOperation(new Dictionary<string, string>()
{ { "orders/1-A", changeVector1 }, { "users/999", changeVector2 } }));
}
Syntax
Available overloads:
====================
public RevertRevisionsByIdOperation(string id, string cv);
public RevertRevisionsByIdOperation(Dictionary<string, string> idToChangeVector);
Parameter | Type | Description |
---|---|---|
id | string |
The ID of the document to revert. |
cv | string |
The change vector of the revision to which the document should be reverted. |
idToChangeVector | Dictionary<string, string> |
A dictionary where each key is a document ID, and each value is the change-vector of the revision to which the document should be reverted. |