Compare Exchange Metadata

  • RavenDB 5.0 added metadata to compare exchange values.

  • In this page:


A compare exchange value's metadata is very similar to a document's metadata.

The metadata can be used to set compare exchange expiration.

The metadata is accessible as a root property of the compare exchange value object:

using (IAsyncDocumentSession session = store.OpenAsyncSession(
                                       new SessionOptions { 
                                           TransactionMode = TransactionMode.ClusterWide 
    // Create a new compare exchange value
    var cmpxchgValue = session.Advanced.ClusterTransaction.CreateCompareExchangeValue("key", "value");

    // Add a field to the metadata
    // with a value of type string
    cmpxchgValue.Metadata["Field name"] = "some value";

    // Retrieve metadata as a dictionary
    IDictionary<string, object> cmpxchgMetadata = cmpxchgValue.Metadata;

You can send it as a parameter of the Put Compare Exchange Value operation:

// Create some metadata
var cmpxchgMetadata = new MetadataAsDictionary {
                          ["Year"] = "2020" 

// Create/update the compare exchange value "best"
await documentStore.Operations.SendAsync(
    new PutCompareExchangeValueOperation<User>(
        new User { Name = "Alice" },