March 23, 2025
+ 12
How to read documents with a command line tool or an SDK.

Introduction

Retrieving documents by ID is the fastest and simplest way to read data in Couchbase Capella. The Key-Value (KV) or Data Service allows you to retrieve a full document when you need to fetch all of the data stored. However, in instances where this can be costly and unnecessary, Couchbase also provides access to specific paths within a document.

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.

Reading a Document

To read a single document, perform a get operation.

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

  2. Use the doc get command to retrieve a document by ID and output its data.

An object is returned, which includes the id, content, and other metadata. The document itself is wrapped in the content field.


The example below retrieves document hotel-123 from the hotel keyspace in the inventory scope.

cb-env bucket travel-sample
cb-env scope inventory
cb-env collection hotel

doc get hotel-123
Result
console
╭───┬───────────┬────────────────────┬─────────────────────┬───────┬─────────╮ │ # │ id │ content │ cas │ error │ cluster │ ├───┼───────────┼────────────────────┼─────────────────────┼───────┼─────────┤ │ 0 │ hotel-123 │ {record 11 fields} │ 1717190781443309568 │ │ capella │ ╰───┴───────────┴────────────────────┴─────────────────────┴───────┴─────────╯
If the document cannot be found, Couchbase Shell returns a Key not found error.

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

Reading with Options

To specify further parameters, add options to the get operation.

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

  2. Use the doc get command to retrieve a document by ID, and pass options as required.


The example below pipes the hotel-123 document and its metadata through the to json filter to transform the output to JSON.

cb-env bucket travel-sample
cb-env scope inventory
cb-env collection hotel

doc get hotel-123 | to json
Result
json
[ { "id": "hotel-123", "content": { "address": "Capstone Road, ME7 3JE", "city": "Medway", "country": "United Kingdom", "description": "40 bed summer hostel about 3 miles from Gillingham.", "geo": { "accuracy": "RANGE_INTERPOLATED", "lat": 51.35785, "lon": 0.55818 }, "id": 123, "name": "Medway Youth Hostel", "reviews": [ { "author": "Ozella Sipes", "content": "This was our 2nd trip here and we enjoyed it more than last year.", "date": "2021-11-17T17:35:05.351Z" } ], "state": null, "url": "http://www.yha.org.uk", "vacancy": true }, "cas": 1717190781443309568, "error": "", "cluster": "capella" } ]

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

Reading a Sub-Document

JSON documents can contain a lot of nested data — which might not necessarily need to be accessed all at once. For example, the document airport_1254 contains a sub-document called geo.

airport_1254
json
{ "id": 1254, // ... "geo": { "lat": 50.962097, "lon": 1.954764, "alt": 12 } }

Reading full documents to access a field or two is not ideal and could cause performance issues in your application. Instead, a better practice would be to access specific paths, or sub-documents, to perform more efficient read operations. To fetch a specific field inside a document, you can perform a sub-document get operation.

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

  2. Use the doc get command to retrieve a document by ID.

  3. Pipe the document through the get filter and specify the path to the field containing the sub-document.


The example below fetches the geo data from the hotel-123 document.

cb-env bucket travel-sample
cb-env scope inventory
cb-env collection hotel

doc get hotel-123 | get content.geo
Result
console
╭───┬────────────────────┬─────────┬────────╮ │ # │ accuracy │ lat │ lon │ ├───┼────────────────────┼─────────┼────────┤ │ 0 │ RANGE_INTERPOLATED │ 51.3578 │ 0.5582 │ ╰───┴────────────────────┴─────────┴────────╯
If the field containing the sub-document cannot be found, the get command returns a Cannot find column error.

For further details, refer to get for filters in the Nushell documentation.

Key-Value Operations with SDKs:

Sub-Document operations with SDKs: