March 23, 2025
+ 12
How to perform bulk CRUD operations with a command line tool or an SDK.

Introduction

Performing bulk operations with Couchbase Capella can be done in several ways, depending on the SDK or command line tool used to perform them. This guide contains basic procedures for performing bulk CRUD operations on documents.

Read the following for further information about the clients available:

Please note that the examples in this guide will alter the data in your sample database. To restore your sample data, remove and reinstall the travel sample data. Refer to Import Data with the Capella UI for details.

Creating Multiple Documents

To create multiple documents, perform a bulk insert operation.

  1. If you haven’t already done so, use cb-env to set the bucket, scope, and collection in which to store the documents.

  2. Create an array of structured documents to insert.

  3. Pipe each item through the wrap filter to wrap it in a content field.

  4. Pipe each item through the insert filter to add an id field with a unique value: for example, by copying a unique field from the content.

  5. Pipe the output through the doc insert command to create the documents.


The example below inserts multiple JSON documents in the users keyspace in the tenant_agent_00 scope.

cb-env bucket travel-sample
cb-env scope tenant_agent_00
cb-env collection users

[
  {id: "user_111", email: "tom_the_cat@example.com"},
  {id: "user_222", email: "jerry_mouse@example.com"},
  {id: "user_333", email: "mickey_mouse@example.com"}
] | wrap content | insert id { |this| echo $this.content.id } | doc insert
Result
console
╭───┬───────────┬─────────┬────────┬──────────┬─────────╮ │ # │ processed │ success │ failed │ failures │ cluster │ ├───┼───────────┼─────────┼────────┼──────────┼─────────┤ │ 0 │ 3 │ 3 │ 0 │ │ capella │ ╰───┴───────────┴─────────┴────────┴──────────┴─────────╯

For further details, refer to Mutating in the Couchbase Shell documentation.

Reading Multiple Documents

To read multiple documents, perform a bulk get operation.

  1. If you haven’t already done so, use cb-env to set the bucket, scope, and collection where the documents are stored.

  2. Create an array of document IDs.

  3. Pipe each document ID through the wrap filter to wrap it in an id field.

  4. Pipe the output through the doc get command to retrieve multiple documents by their IDs.


The example below fetches multiple JSON documents from the users keyspace in the tenant_agent_00 scope.

cb-env bucket travel-sample
cb-env scope tenant_agent_00
cb-env collection users

['0' '1'] | wrap id | doc get
Result
console
╭───┬────┬────────────────────┬─────────────────────┬───────┬─────────╮ │ # │ id │ content │ cas │ error │ cluster │ ├───┼────┼────────────────────┼─────────────────────┼───────┼─────────┤ │ 0 │ 1 │ {record 11 fields} │ 1717505682500419584 │ │ capella │ │ 1 │ 0 │ {record 11 fields} │ 1717505682774949888 │ │ capella │ ╰───┴────┴────────────────────┴─────────────────────┴───────┴─────────╯

For further details, refer to Reading in the Couchbase Shell documentation.

Updating Multiple Documents

To update multiple documents, perform a bulk upsert or replace operation.

  1. If you haven’t already done so, use cb-env to set the bucket, scope, and collection where the documents are stored.

  2. Create an array of updated structured documents.

  3. Pipe each item through the wrap filter to wrap it in a content field.

  4. Pipe each item through the insert filter to add an id field with a unique value: for example, by copying a unique field from the content.

  5. Pipe the output through the doc upsert or doc replace command to update the documents.


The example below upserts multiple JSON documents in the users keyspace in the tenant_agent_00 scope.

cb-env bucket travel-sample
cb-env scope tenant_agent_00
cb-env collection users

[
  {id: "user_111", email: "tom@example.com"},
  {id: "user_222", email: "jerry@example.com"},
  {id: "user_333", email: "mickey@example.com"}
] | wrap content | insert id { |this| echo $this.content.id } | doc upsert
Result
console
╭───┬───────────┬─────────┬────────┬──────────┬─────────╮ │ # │ processed │ success │ failed │ failures │ cluster │ ├───┼───────────┼─────────┼────────┼──────────┼─────────┤ │ 0 │ 3 │ 3 │ 0 │ │ capella │ ╰───┴───────────┴─────────┴────────┴──────────┴─────────╯

For further details, refer to Mutating in the Couchbase Shell documentation.

Deleting Multiple Documents

To delete multiple documents, perform a bulk remove operation.

  1. If you haven’t already done so, use cb-env to set the bucket, scope, and collection where the documents are stored.

  2. Create an array of document IDs.

  3. Pipe each document ID through the wrap filter to wrap it in an id field.

  4. Pipe the output through the doc remove command to delete multiple documents by their IDs.


The example below deletes multiple JSON documents from the users keyspace in the tenant_agent_00 scope.

cb-env bucket travel-sample
cb-env scope tenant_agent_00
cb-env collection users

[user_111 user_222 user_333] | wrap id | doc remove
Result
console
╭───┬───────────┬─────────┬────────┬──────────┬─────────╮ │ # │ processed │ success │ failed │ failures │ cluster │ ├───┼───────────┼─────────┼────────┼──────────┼─────────┤ │ 0 │ 3 │ 3 │ 0 │ │ capella │ ╰───┴───────────┴─────────┴────────┴──────────┴─────────╯

For further details, refer to Removing in the Couchbase Shell documentation.

Bulk Operations with SDKs: