---
title: UPDATE
slug: v4-5/crud-update
docTags: 
createdAt: 2023-11-14T21:13:44.225Z
---

This article provides an overview of the updating documents using the `UPDATE` and `INSERT` DQL operations.

All modification operations in Ditto are performed using the `execute` API method against the `ditto.store`:

:::CodeblockTabs
```swift
let result = try await ditto.store.execute(query: /* query */, arguments: /* arguments */);
```

```kotlin
var result = ditto.store.execute(/* query */, /* arguments */)
```

```javascript
const result = await ditto.store.execute(/* query */, /* arguments */)
```

```java
DittoQueryResult result = (DittoQueryResult) ditto.store.execute(
  /* query */,
  /* arguments */,
  /* continuation */);
```

```csharp
var result = await ditto.Store.ExecuteAsync(/* query */, /* arguments */);
```

```cpp
auto result = ditto.get_store().execute(/* query */, /* arguments */).get();
```

```rust
let result = ditto.store().execute(/* query */, /* arguments */);
```
:::

# Updating with UPDATE

Update operations ensure that only the minimum data necessary to enforce all peers converge on one view of the data replicates across the mesh:

:::CodeblockTabs
```swift
try await ditto.store.execute("""
  UPDATE cars
  SET color = 'blue'
  WHERE _id = '123'
  """);
```

```kotlin
ditto.store.execute("""
  UPDATE cars
  SET color = 'blue'
  WHERE _id = '123'
  """)
```

```javascript
await ditto.store.execute(`
  UPDATE cars
  SET color = 'blue'
  WHERE _id = '123'`)
```

```java
DittoQueryResult result = (DittoQueryResult) ditto.store.execute(
  "UPDATE cars SET color = 'blue' WHERE _id = '123'",
  new Continuation<>() {
    @NonNull
    @Override
    public CoroutineContext getContext() {
      return EmptyCoroutineContext.INSTANCE;
    }

    @Override
    public void resumeWith(@NonNull Object o) {
      if (o instanceof Result.Failure) {
        // Handle failure
      }
    }
  }
);
```

```csharp
await ditto.Store.ExecuteAsync(
  "UPDATE cars SET color = 'blue' WHERE _id = '123'");
```

```cpp
ditto.get_store().execute(
  "UPDATE cars SET color = 'blue' WHERE _id = '123'").get();
```

```rust
ditto.store().execute(
  "UPDATE cars SET color = 'blue' WHERE _id = '123'",
  None); 
```
:::

For complete DQL syntax, see *Ditto Query Language (DQL)&#x20;*> [UPDATE](docId\:t3hF69sxFGUv3v2olE3rz)

## Updating Multiple Documents in a Collection

`UPDATE` operates over a condition that allows multiple documents to be updated at the same time.

In the following example, all the `red` cars in the `cars` collection are updated to be `blue`. The documents updated can be referenced by using the `mutatedDocumentIDs` method on the `result`.

:::CodeblockTabs
```swift
let result = try await ditto.store.execute(
  "UPDATE cars SET color = 'blue' WHERE _id = '123'");
  
result.mutatedDocumentIDs.forEach() { print($0) }
```

```kotlin
var result = ditto.store.execute("""
  UPDATE cars
  SET color = 'blue'
  WHERE _id = '123'
  """)

result.mutatedDocumentIDs().forEach { id ->
    println(id)
}
```

```javascript
const result = await ditto.store.execute(`
  UPDATE cars
  SET color = 'blue'
  WHERE color = 'red'`)

console.log(result.mutatedDocumentIDs())
```

```java
DittoQueryResult result = (DittoQueryResult) ditto.store.execute(
    "UPDATE cars SET color = 'blue' WHERE color = 'red'",
    new Continuation<>() {
        @NonNull
        @Override
        public CoroutineContext getContext() {
            return EmptyCoroutineContext.INSTANCE;
        }

        @Override
        public void resumeWith(@NonNull Object o) {
            if (o instanceof Result.Failure) {
                // Handle failure
            }
        }
    }
);

for (String id : result.mutatedDocumentIDs()) {
    System.out.println(id);
}
```

```csharp
const result = await ditto.Store.ExecuteAsync(
  "UPDATE cars SET color = 'blue' WHERE color = 'red'");
  
result.MutatedDocumentIDs.ForEach(id => Console.WriteLine(id));
```

```cpp
ditto.get_store().execute(
  "UPDATE cars SET color = 'blue' WHERE color = 'red'").get();
```

```rust
ditto.store().execute(
  "UPDATE cars SET color = 'blue' WHERE color = 'red'",
  None); 
```
:::

# Updating with INSERT

The `INSERT` operation provides conflict policy options to override default behavior if a document with the same ID already exists. By using the `ON ID CONFLICT DO UPDATE` policy, inserted documents automatically apply updates for all provided fields.

:::hint{type="warning"}
Updating data using an `INSERT` operation may cause performance to degrade. This is because when you use an INSERT operation to modify data, all provided fields update, even if they remain unchanged.&#x20;

To optimize performance and reduce unnecessary overhead, apply most updates in your app through the `UPDATE` method instead.
:::

:::CodeblockTabs
```swift
var document = [
  "_id": "123",
  "color": "red",
];

try await ditto.store.execute(
  query:"""
  INSERT INTO cars
  DOCUMENTS (:document)
  ON ID CONFLICT DO UPDATE
  """,
  arguments: [ "document": document ]);
```

```kotlin
ditto.store.execute(
  "INSERT INTO cars DOCUMENTS (:car)",
  mapOf("car" to mapOf(
    "_id" to "123",
    "color" to "red"
  )))
```

```javascript
const document = {
  _id: "123",
  color: "red",
}

await ditto.store.execute(`
  INSERT INTO cars
  DOCUMENTS (:document)
  ON ID CONFLICT DO UPDATE`,
  { document })
```

```java
Map<String, String> document = new HashMap<>();
newCar.put("_id", "123");
newCar.put("color", "red");

DittoQueryResult result = (DittoQueryResult) ditto.store.execute(
    "INSERT INTO cars DOCUMENTS (:document) ON ID CONFLICT DO UPDATE",
    Collections.singletonMap("document", document),
    new Continuation<>() {
        @NonNull
        @Override
        public CoroutineContext getContext() {
            return EmptyCoroutineContext.INSTANCE;
        }

        @Override
        public void resumeWith(@NonNull Object o) {
            if (o instanceof Result.Failure) {
                // Handle failure
            }
        }
    }
);
```

```csharp
var args = new Dictionary<string, object> {
  "document", new { _id = "123" , color = "red" }
};

await ditto.Store.ExecuteAsync(
  "INSERT INTO cars"
+ " DOCUMENTS (:document) ON ID CONFLICT DO UPDATE",
  args);
```

```cpp
std::map<std::string, std::map<std::string, std::string>> args;
args["document"] = {{"_id", "123"},{"color", "red"}};

ditto.get_store().execute(
  "INSERT INTO cars DOCUMENTS (:document) ON ID CONFLICT DO UPDATE",
  args).get();
```

```rust
struct Args {
  document: Car,
}
struct Car {
  _id: String,
  color: String
}

// ...

let args = Args {
  document: Car {
    _id: "123".to_string(),
    color: "red".to_string()
  },
};

ditto.store().execute(
  "INSERT INTO cars DOCUMENTS (:document)",
  args); 
```
:::

For complete DQL syntax, see *Ditto Query Language (DQL)&#x20;*> [INSERT](docId\:poKcNKEX_OxyeJKOAUPTi)
