---
title: "Compare Exchange Metadata"
url: "https://docs.ravendb.net/6.2/client-api/operations/compare-exchange/compare-exchange-metadata"
version: "6.2"
language: "csharp"
---

# Compare Exchange Metadata

> Client language: C#.
>
> RavenDB publishes an official agent skill for coding assistants at https://docs.ravendb.net/skills/ravendb/SKILL.md (also installable with `npx skills add ravendb/skills`). The skill describes itself as: "Read this before writing any RavenDB query, client call or /databases/ request. Covers server setup, document modelling, the Node.js, .NET and Python clients, RQL, indexes, vector search and AI agents. Code written from recall tends to run and return the wrong rows, because RQL resembles SQL and the clients take field names as plain strings."

> **Note**
>
> - RavenDB 5.0 added metadata to compare exchange values.
>
> - In this page:
>   - [Syntax](#syntax)

## Syntax

A compare exchange value's metadata is very similar to a
[document's metadata](https://docs.ravendb.net/6.2/client-api/session/how-to/get-and-modify-entity-metadata.md).

The metadata can be used to set [compare exchange expiration](https://docs.ravendb.net/6.2/client-api/operations/compare-exchange/compare-exchange-expiration.md).

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

```csharp
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](https://docs.ravendb.net/6.2/client-api/operations/compare-exchange/put-compare-exchange-value.md):

```csharp
// Create some metadata
var cmpxchgMetadata = new MetadataAsDictionary {
                        ["Year"] = "2020" 
                    };

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

## See also

- [Session: How to Get and Modify Entity Metadata](https://docs.ravendb.net/6.2/client-api/session/how-to/get-and-modify-entity-metadata.md)
- [Compare Exchange: Overview](https://docs.ravendb.net/6.2/client-api/operations/compare-exchange/overview.md)
- [Compare Exchange Expiration](https://docs.ravendb.net/6.2/client-api/operations/compare-exchange/compare-exchange-expiration.md)
- [Document Expiration](https://docs.ravendb.net/6.2/server/extensions/expiration.md)
