Reference documentation and code samples for the Cloud Spanner API class Google::Cloud::Spanner::BatchWrite.
Part of the BatchWrite DSL.
This object is passed as a parameter to the block passed to
Client#batch_write. Use this parameter to add
mutation groups to the batch.
Inherits
Object
Methods
#mutation_group
defmutation_group()
Adds a group of mutations
Yields
(mg)
Example
require"google/cloud/spanner"spanner=Google::Cloud::Spanner.newdb=spanner.client"my-instance","my-database"results=db.batch_writedo|b|# First mutation groupb.mutation_groupdo|mg|mg.upsert"Singers",[{SingerId:16,FirstName:"Charlie",LastName:"Terry"}]end# Second mutation groupb.mutation_groupdo|mg|mg.upsert"Singers",[{SingerId:17,FirstName:"Catalina",LastName:"Smith"}]mg.update"Albums",[{SingerId:17,AlbumId:1,AlbumTitle:"Go Go Go"}]endend
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-28 UTC."],[],[],null,["# Cloud Spanner API - Class Google::Cloud::Spanner::BatchWrite (v2.27.0)\n\nVersion latestkeyboard_arrow_down\n\n- [2.27.0 (latest)](/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-BatchWrite)\n- [2.26.0](/ruby/docs/reference/google-cloud-spanner/2.26.0/Google-Cloud-Spanner-BatchWrite)\n- [2.25.0](/ruby/docs/reference/google-cloud-spanner/2.25.0/Google-Cloud-Spanner-BatchWrite)\n- [2.24.0](/ruby/docs/reference/google-cloud-spanner/2.24.0/Google-Cloud-Spanner-BatchWrite)\n- [2.23.0](/ruby/docs/reference/google-cloud-spanner/2.23.0/Google-Cloud-Spanner-BatchWrite)\n- [2.22.0](/ruby/docs/reference/google-cloud-spanner/2.22.0/Google-Cloud-Spanner-BatchWrite)\n- [2.21.0](/ruby/docs/reference/google-cloud-spanner/2.21.0/Google-Cloud-Spanner-BatchWrite)\n- [2.20.0](/ruby/docs/reference/google-cloud-spanner/2.20.0/Google-Cloud-Spanner-BatchWrite)\n- [2.19.1](/ruby/docs/reference/google-cloud-spanner/2.19.1/Google-Cloud-Spanner-BatchWrite)\n- [2.18.1](/ruby/docs/reference/google-cloud-spanner/2.18.1/Google-Cloud-Spanner-BatchWrite)\n- [2.17.0](/ruby/docs/reference/google-cloud-spanner/2.17.0/Google-Cloud-Spanner-BatchWrite)\n- [2.16.1](/ruby/docs/reference/google-cloud-spanner/2.16.1/Google-Cloud-Spanner-BatchWrite)\n- [2.15.0](/ruby/docs/reference/google-cloud-spanner/2.15.0/Google-Cloud-Spanner-BatchWrite)\n- [2.14.0](/ruby/docs/reference/google-cloud-spanner/2.14.0/Google-Cloud-Spanner-BatchWrite)\n- [2.13.0](/ruby/docs/reference/google-cloud-spanner/2.13.0/Google-Cloud-Spanner-BatchWrite)\n- [2.12.1](/ruby/docs/reference/google-cloud-spanner/2.12.1/Google-Cloud-Spanner-BatchWrite)\n- [2.11.0](/ruby/docs/reference/google-cloud-spanner/2.11.0/Google-Cloud-Spanner-BatchWrite) \nReference documentation and code samples for the Cloud Spanner API class Google::Cloud::Spanner::BatchWrite.\n\nPart of the BatchWrite DSL.\n\n\n\u003cbr /\u003e\n\nThis object is passed as a parameter to the block passed to\n[Client#batch_write](/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)\"). Use this parameter to add\nmutation groups to the batch. \n\nInherits\n--------\n\n- Object\n\nMethods\n-------\n\n### #mutation_group\n\n def mutation_group()\n\nAdds a group of mutations \n**Yields**\n\n- (mg)\n**Example** \n\n```ruby\nrequire \"google/cloud/spanner\"\n\nspanner = Google::Cloud::Spanner.new\n\ndb = spanner.client \"my-instance\", \"my-database\"\n\nresults = db.batch_write do |b|\n # First mutation group\n b.mutation_group do |mg|\n mg.upsert \"Singers\", [{ SingerId: 16, FirstName: \"Charlie\", LastName: \"Terry\" }]\n end\n\n # Second mutation group\n b.mutation_group do |mg|\n mg.upsert \"Singers\", [{ SingerId: 17, FirstName: \"Catalina\", LastName: \"Smith\" }]\n mg.update \"Albums\", [{ SingerId: 17, AlbumId: 1, AlbumTitle: \"Go Go Go\" }]\n end\nend\n```"]]