A newer version of this documentation is available.

View Latest
February 16, 2025
+ 12

How to perform bulk CRUD operations in Couchbase.
This guide is for Couchbase Server.

Introduction

Performing bulk operations with Couchbase Server 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 Couchbase 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 Sample Buckets for details.

Creating Multiple Documents

To create multiple documents in Couchbase perform a bulk insert operation.

  1. Create some structured JSON documents to insert.

  2. Initialize a list of IMutationResult tasks.

  3. Perform an insert operation on each document and store the result in the tasks list.

  4. Wait for all the tasks to complete before accessing the results.


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

csharp
var documents = new[] { new { id = "user_111", email = "tom_the_cat@gmail.com"}, new { id = "user_222", email = "jerry_mouse@gmail.com"}, new { id = "user_333", email = "mickey_mouse@gmail.com"} }; // Collection of things that will complete in the future. var tasks = new List<Task<IMutationResult>>(); // Create tasks to be executed concurrently. foreach (var document in documents) { Console.WriteLine($"Inserting document: {document.id}"); var task = usersCollection.InsertAsync(document.id, document); tasks.Add(task); } // Wait until all of the tasks have completed. await Task.WhenAll(tasks); // Iterate task list to get results. foreach (var task in tasks) Console.WriteLine($"CAS: {task.Result.Cas}");

Click the View button to see this code in context.

For further details, refer to CollectionExtensions.

Reading Multiple Documents

To read multiple documents in Couchbase perform a bulk get operation.

Use the cbc cat 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.

cbc cat -u Administrator -P password -U couchbase://localhost/travel-sample \
	--scope='tenant_agent_00' \
	--collection='users' \
	0 1
Result
console
0 CAS=0x16be2392ca2e0000, Flags=0x0, Size=904, Datatype=0x01(JSON) { "name": "Keon Hoppe", "addresses": [ { "type": "home", "address": "222 Sauer Neck", "city": "London", "country": "United Kingdom" }, ], ... } 1 CAS=0x16be2392c9870000, Flags=0x0, Size=697, Datatype=0x01(JSON) { "name": "Rigoberto Bernier", "addresses": [ { "type": "home", "address": "0622 Adams Mills", "city": "Manchester", "country": "United Kingdom" } ], ... }

For further details, refer to cbc(1).

Updating Multiple Documents

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

  1. Add new data to update some existing JSON documents.

  2. Initialize a list of IMutationResult tasks.

  3. Perform an upsert or replace operation on each document and store the results in the tasks list.

  4. Wait for all the tasks to complete before accessing the results.


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

csharp
var documents = new[] { new { id = "user_111", email = "tom_the_cat@gmail.com"}, new { id = "user_222", email = "jerry_mouse@gmail.com"}, new { id = "user_333", email = "mickey_mouse@gmail.com"} }; // Collection of things that will complete in the future. var tasks = new List<Task<IMutationResult>>(); // Create tasks to be executed concurrently. foreach (var document in documents) { Console.WriteLine($"Inserting document: {document.id}"); var task = usersCollection.InsertAsync(document.id, document); tasks.Add(task); } // Wait until all of the tasks have completed. await Task.WhenAll(tasks); // Iterate task list to get results. foreach (var task in tasks) Console.WriteLine($"CAS: {task.Result.Cas}");

Click the View button to see this code in context.

For further details, refer to CollectionExtensions.

Deleting Multiple Documents

To delete multiple documents in Couchbase perform a bulk remove operation.

  1. Initialize a list of tasks.

  2. Perform a remove operation on each document and store the results in the tasks list.

  3. Wait for all the tasks to complete.


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

csharp
var documents = new[] { new { id = "user_111", email = "tom_the_cat@gmail.com"}, new { id = "user_222", email = "jerry_mouse@gmail.com"}, new { id = "user_333", email = "mickey_mouse@gmail.com"} }; // Collection of things that will complete in the future. var tasks = new List<Task>(); // Create tasks to be executed concurrently. foreach (var document in documents) { Console.WriteLine($"Removing document: {document.id}"); var task = usersCollection.RemoveAsync(document.id); tasks.Add(task); } // Wait until all of the tasks have completed. // NOTE: RemoveAsync returns void, so no need to loop through each task. await Task.WhenAll(tasks);

Click the View button to see this code in context.

For further details, refer to CollectionExtensions.

Bulk Operations with SDKs: