# Cloud Spanner API - Class Google::Cloud::Spanner::MutationGroup (v2.36.0)

2.36.0 (latest) 2.35.0 2.34.0 2.33.0 2.32.0 2.31.0 2.30.0 2.29.0 2.28.0 2.27.0 2.26.0 2.25.0 2.24.0 2.23.0 2.22.0 2.21.0 2.20.0 2.19.1 2.18.1 2.17.0 2.16.1 2.15.0 2.14.0 2.13.0 2.12.1 2.11.0 Reference documentation and code samples for the Cloud Spanner API class Google::Cloud::Spanner::MutationGroup.

Part of the BatchWrite DSL. This object defines a group of mutations
to be included in a BatchWrite operation. All writes within a mutation
group will execute atomically at a single logical point in time across
columns, rows, and tables in a database.


<br />

All changes are accumulated in memory until the block passed to
[Client#batch_write](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Client#Google__Cloud__Spanner__Client_batch_write_instance_ "Google::Cloud::Spanner::Client#batch_write (method)") completes.

## Inherits

- Object

## Example

```ruby
require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new

db = spanner.client "my-instance", "my-database"

results = db.batch_write do |b|
  # First mutation group
  b.mutation_group do |mg|
    mg.upsert "Singers", [{ SingerId: 16, FirstName: "Charlie", LastName: "Terry" }]
  end

  # Second mutation group
  b.mutation_group do |mg|
    mg.upsert "Singers", [{ SingerId: 17, FirstName: "Catalina", LastName: "Smith" }]
    mg.update "Albums", [{ SingerId: 17, AlbumId: 1, AlbumTitle: "Go Go Go" }]
  end
end
```

## Methods

### #delete

    def delete(table, keys = [])

Deletes rows from a table. Succeeds whether or not the specified rows
were present.


<br />

All changes are accumulated in memory until the block passed to
[Client#batch_write](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Client#Google__Cloud__Spanner__Client_batch_write_instance_ "Google::Cloud::Spanner::Client#batch_write (method)") completes.
**Parameters**

- **table** (String) --- The name of the table in the database to be modified.
- **keys** (Object, Array\<Object\>) --- A single, or list of keys or key ranges to match returned data to. Values should have exactly as many elements as there are columns in the primary key.
**Example**

```ruby
require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new

db = spanner.client "my-instance", "my-database"

results = db.batch_write do |b|
  b.mutation_group do |mg|
    mg.delete "Singers", [1, 2, 3]
  end
end
```

### #insert

    def insert(table, *rows)

Inserts new rows in a table. If any of the rows already exist, the
write or request fails with error AlreadyExistsError.


<br />

All changes are accumulated in memory until the block passed to
[Client#batch_write](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Client#Google__Cloud__Spanner__Client_batch_write_instance_ "Google::Cloud::Spanner::Client#batch_write (method)") completes.
**Parameters**

- **table** (String) --- The name of the table in the database to be modified.
- **rows** (Array\<Hash\>) --- One or more hash objects with the hash keys matching the table's columns, and the hash values matching the table's values.


  Ruby types are mapped to Spanner types as follows:

  \| Spanner \| Ruby \| Notes \|
  \|---\|---\|---\|
  \| `BOOL` \| `true`/`false` \| \|
  \| `INT64` \| `Integer` \| \|
  \| `FLOAT64` \| `Float` \| \|
  \| `FLOAT32` \| `Float` \| \|
  \| `NUMERIC` \| `BigDecimal` \| \|
  \| `STRING` \| `String` \| \|
  \| `DATE` \| `Date` \| \|
  \| `TIMESTAMP` \| `Time`, `DateTime` \| \|
  \| `BYTES` \| `File`, `IO`, `StringIO`, or similar \| \|
  \| `ARRAY` \| `Array` \| Nested arrays are not supported. \|

  See [Data
  types](https://cloud.google.com/spanner/docs/data-definition-language#data_types).
**Example**

```ruby
require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new

db = spanner.client "my-instance", "my-database"

results = db.batch_write do |b|
  b.mutation_group do |mg|
    mg.insert "Singers", [{ SingerId: 16, FirstName: "Charlie", LastName: "Terry" }]
  end
end
```

### #replace

    def replace(table, *rows)

Inserts or replaces rows in a table. If any of the rows already exist,
it is deleted, and the column values provided are inserted instead.
Unlike #upsert, this means any values not explicitly written become
`NULL`.


<br />

All changes are accumulated in memory until the block passed to
[Client#batch_write](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Client#Google__Cloud__Spanner__Client_batch_write_instance_ "Google::Cloud::Spanner::Client#batch_write (method)") completes.
**Parameters**

- **table** (String) --- The name of the table in the database to be modified.
- **rows** (Array\<Hash\>) --- One or more hash objects with the hash keys matching the table's columns, and the hash values matching the table's values.


  Ruby types are mapped to Spanner types as follows:

  \| Spanner \| Ruby \| Notes \|
  \|---\|---\|---\|
  \| `BOOL` \| `true`/`false` \| \|
  \| `INT64` \| `Integer` \| \|
  \| `FLOAT64` \| `Float` \| \|
  \| `FLOAT32` \| `Float` \| \|
  \| `NUMERIC` \| `BigDecimal` \| \|
  \| `STRING` \| `String` \| \|
  \| `DATE` \| `Date` \| \|
  \| `TIMESTAMP` \| `Time`, `DateTime` \| \|
  \| `BYTES` \| `File`, `IO`, `StringIO`, or similar \| \|
  \| `ARRAY` \| `Array` \| Nested arrays are not supported. \|

  See [Data
  types](https://cloud.google.com/spanner/docs/data-definition-language#data_types).
**Example**

```ruby
require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new

db = spanner.client "my-instance", "my-database"

results = db.batch_write do |b|
  b.mutation_group do |mg|
    mg.replace "Singers", [{ SingerId: 16, FirstName: "Charlie", LastName: "Terry" }]
  end
end
```

### #save

    def save(table, *rows)

**Alias Of** : [#upsert](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-MutationGroup#Google__Cloud__Spanner__MutationGroup_upsert_instance_) Inserts or updates rows in a table. If any of the rows already exist,
then its column values are overwritten with the ones provided. Any
column values not explicitly written are preserved.


<br />

All changes are accumulated in memory until the block passed to
[Client#batch_write](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Client#Google__Cloud__Spanner__Client_batch_write_instance_ "Google::Cloud::Spanner::Client#batch_write (method)") completes.
**Parameters**

- **table** (String) --- The name of the table in the database to be modified.
- **rows** (Array\<Hash\>) --- One or more hash objects with the hash keys matching the table's columns, and the hash values matching the table's values.


  Ruby types are mapped to Spanner types as follows:

  \| Spanner \| Ruby \| Notes \|
  \|---\|---\|---\|
  \| `BOOL` \| `true`/`false` \| \|
  \| `INT64` \| `Integer` \| \|
  \| `FLOAT64` \| `Float` \| \|
  \| `FLOAT32` \| `Float` \| \|
  \| `NUMERIC` \| `BigDecimal` \| \|
  \| `STRING` \| `String` \| \|
  \| `DATE` \| `Date` \| \|
  \| `TIMESTAMP` \| `Time`, `DateTime` \| \|
  \| `BYTES` \| `File`, `IO`, `StringIO`, or similar \| \|
  \| `ARRAY` \| `Array` \| Nested arrays are not supported. \|

  See [Data
  types](https://cloud.google.com/spanner/docs/data-definition-language#data_types).
**Example**

```ruby
require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new

db = spanner.client "my-instance", "my-database"

results = db.batch_write do |b|
  b.mutation_group do |mg|
    mg.upsert "Singers", [{ SingerId: 16, FirstName: "Charlie", LastName: "Terry" }]
  end
end
```

### #update

    def update(table, *rows)

Updates existing rows in a table. If any of the rows does not already
exist, the request fails with error NotFoundError.


<br />

All changes are accumulated in memory until the block passed to
[Client#batch_write](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Client#Google__Cloud__Spanner__Client_batch_write_instance_ "Google::Cloud::Spanner::Client#batch_write (method)") completes.
**Parameters**

- **table** (String) --- The name of the table in the database to be modified.
- **rows** (Array\<Hash\>) --- One or more hash objects with the hash keys matching the table's columns, and the hash values matching the table's values.


  Ruby types are mapped to Spanner types as follows:

  \| Spanner \| Ruby \| Notes \|
  \|---\|---\|---\|
  \| `BOOL` \| `true`/`false` \| \|
  \| `INT64` \| `Integer` \| \|
  \| `FLOAT64` \| `Float` \| \|
  \| `FLOAT32` \| `Float` \| \|
  \| `NUMERIC` \| `BigDecimal` \| \|
  \| `STRING` \| `String` \| \|
  \| `DATE` \| `Date` \| \|
  \| `TIMESTAMP` \| `Time`, `DateTime` \| \|
  \| `BYTES` \| `File`, `IO`, `StringIO`, or similar \| \|
  \| `ARRAY` \| `Array` \| Nested arrays are not supported. \|

  See [Data
  types](https://cloud.google.com/spanner/docs/data-definition-language#data_types).
**Example**

```ruby
require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new

db = spanner.client "my-instance", "my-database"

results = db.batch_write do |b|
  b.mutation_group do |mg|
    mg.update "Singers", [{ SingerId: 16, FirstName: "Charlie", LastName: "Terry" }]
  end
end
```

### #upsert

    def upsert(table, *rows)

**Aliases**

- [#save](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-MutationGroup#Google__Cloud__Spanner__MutationGroup_save_instance_)
Inserts or updates rows in a table. If any of the rows already exist,
then its column values are overwritten with the ones provided. Any
column values not explicitly written are preserved.


<br />

All changes are accumulated in memory until the block passed to
[Client#batch_write](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Client#Google__Cloud__Spanner__Client_batch_write_instance_ "Google::Cloud::Spanner::Client#batch_write (method)") completes.
**Parameters**

- **table** (String) --- The name of the table in the database to be modified.
- **rows** (Array\<Hash\>) --- One or more hash objects with the hash keys matching the table's columns, and the hash values matching the table's values.


  Ruby types are mapped to Spanner types as follows:

  \| Spanner \| Ruby \| Notes \|
  \|---\|---\|---\|
  \| `BOOL` \| `true`/`false` \| \|
  \| `INT64` \| `Integer` \| \|
  \| `FLOAT64` \| `Float` \| \|
  \| `FLOAT32` \| `Float` \| \|
  \| `NUMERIC` \| `BigDecimal` \| \|
  \| `STRING` \| `String` \| \|
  \| `DATE` \| `Date` \| \|
  \| `TIMESTAMP` \| `Time`, `DateTime` \| \|
  \| `BYTES` \| `File`, `IO`, `StringIO`, or similar \| \|
  \| `ARRAY` \| `Array` \| Nested arrays are not supported. \|

  See [Data
  types](https://cloud.google.com/spanner/docs/data-definition-language#data_types).
**Example**

```ruby
require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new

db = spanner.client "my-instance", "my-database"

results = db.batch_write do |b|
  b.mutation_group do |mg|
    mg.upsert "Singers", [{ SingerId: 16, FirstName: "Charlie", LastName: "Terry" }]
  end
end
```