---
title: "Get Compare Exchange Value Operation"
url: "https://docs.ravendb.net/6.2/client-api/operations/compare-exchange/get-compare-exchange-value"
version: "6.2"
language: "nodejs"
---

# Get Compare Exchange Value Operation

> Client language: Node.js. Also available for [C#](https://docs.ravendb.net/6.2/client-api/operations/compare-exchange/get-compare-exchange-value.md), [Java](https://docs.ravendb.net/6.2/client-api/operations/compare-exchange/get-compare-exchange-value.java.md).
>
> 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**
>
> - Use the `GetCompareExchangeValueOperation` operation to get a compare-exchange item by its *key*.
>
> - Compare-exchange items can also be managed via [advanced session methods](https://docs.ravendb.net/6.2/client-api/session/cluster-transaction/compare-exchange.nodejs.md),  
>   which also expose getting the compare-exchange lazily,  
>   or from the [Studio](https://docs.ravendb.net/6.2/studio/database/documents/compare-exchange-view.md).
>
> - In this page:
>   - [Examples](#examples)
>     - [Get cmpXchg item that has a number and metadata](#get-cmpxchg-item-that-has-a-number-and-metadata)
>     - [Get cmpXchg item that has an object](#get-cmpxchg-item-that-has-an-object)
>   - [Syntax](#syntax)

## Examples

> **Note**
>
> &#x20;**Get cmpXchg item that has a number and metadata**:
>
> ```javascript
> // Put a new compare-exchange item,
> // e.g. save the number of sales made by an employee as the value + some metadata info        
> const putCmpXchgOp = new PutCompareExchangeValueOperation("employees/1-A", 12345, 0,{
>   "Department": "Sales",
>   "Role": "Salesperson",
> });
> const result = await documentStore.operations.send(putCmpXchgOp);
>
> // Get the compare-exchange item:
> // ==============================
>
> // Define the get compare-exchange operation, pass the unique item key
> const getCmpXchgOp = new GetCompareExchangeValueOperation("employees/1-A");
>
> // Execute the operation by passing it to operations.send
> const item = await documentStore.operations.send(getCmpXchgOp);
>
> // Access the value and metadata of the retrieved item
> const numberOfSales = item.value;
> const employeeRole = item.metadata["Role"];
>
> // Access the version number of the retrieved item
> const version = item.index;
> ```

> **Note**
>
> &#x20;**Get cmpXchg item that has an object**:
>
> ### Get\_operation
>
> ```javascript
> // Put a new compare-exchange item,
> // e.g. save an object as the value
> const employee = new Employee();
> employee.role = "Salesperson"
> employee.department = "Sales";
> employee.numberOfSales = 12345;
>
> const putCmpXchgOp = new PutCompareExchangeValueOperation("employees/1-A", employee, 0);
> const result = await documentStore.operations.send(putCmpXchgOp);
>
> // Get the compare-exchange item:
> // ==============================
>
> // Define the get compare-exchange operation, pass the unique item key & the class type
> const getCmpXchgOp = new GetCompareExchangeValueOperation("employees/1-A", Employee);
>
> // Execute the operation by passing it to operations.send
> const item = await documentStore.operations.send(getCmpXchgOp);
>
> // Access the value of the retrieved item
> const employeeResult = item.value;
> const employeeClass = employeeResult.constructor;   // Employee
>
> const employeeRole = employeeResult.role;           // Salesperson
> const employeeDep = employeeResult.department;      // Sales
> const employeeSales = employeeResult.numberOfSales; // 12345
>
> // Access the version number of the retrieved item
> const version = item.index;
> ```
>
> ### Employee\_class
>
> ```javascript
> class Employee {
>   constructor(
>       id = null,
>       department = "",
>       role = "",
>       numberOfSales = 0
>
>   ) {
>       Object.assign(this, {
>           id,
>           department,
>           role,
>           numberOfSales
>       });
>   }
> }
> ```

## Syntax

```javascript
const getCmpXchgOp = new GetCompareExchangeValueOperation(key, clazz, materializeMetadata);
```

| Parameter | Type | Description |
| - | - | - |
| **key** | `string` | The unique identifier of the cmpXchg item. |
| **clazz** | `object` | The class type of the item's value. |
| **materializeMetadata** | `boolean` | The Metadata will be retrieved and available regardless of the value of this param. Used for internal purposes. |

```javascript
// Return value of store.operations.send(getCmpXchgOp)
// ===================================================
class CompareExchangeValue {
  key;
  value;
  metadata;
  index;
}
```

| Parameter | Type | Description |
| - | - | - |
| **key** | `string` | The unique identifier of the cmpXchg item. |
| **value** | `object` | The existing `value` of the returned cmpXchg item. |
| **metadata** | `object` | The existing `metadata` of the returned cmpXchg item. |
| **index** | `number` | The compare-exchange item's version. |

## See also

- [Overview](https://docs.ravendb.net/6.2/client-api/operations/compare-exchange/overview.md)
- [Get Compare-Exchange Values](https://docs.ravendb.net/6.2/client-api/operations/compare-exchange/get-compare-exchange-values.nodejs.md)
- [Put a Compare-Exchange Value](https://docs.ravendb.net/6.2/client-api/operations/compare-exchange/delete-compare-exchange-value.nodejs.md)
- [Cluster Transaction - Overview](https://docs.ravendb.net/6.2/client-api/session/cluster-transaction/overview.nodejs.md)
- [Operations overview](https://docs.ravendb.net/6.2/client-api/operations/what-are-operations.nodejs.md)
