A newer version of this documentation is available.

View Latest

Retrieving documents

Retrieve stored documents from Couchbase
document = get(docid)
This section discusses retrieving documents using their IDs, or primary keys. Documents can also be accessed using secondary lookups via Query and MapReduce. Primary key lookups are performed using the key-value API, which simplifies use and increases performance (as applications may interact with the KV store directly, rather than a secondary index or query processor).

Retrieving documents using the SDK

In Couchbase, documents are stored with their IDs. Retrieving a document via its ID is the simplest and quickest operation in Couchbase.

>>> result = cb.get('docid')
>>> print result.value
{'json': 'value'}

To retrieve a document, use the get() API. This API call will return the document along with metadata (such as the CAS value).

Examples of retrieving a document using an SDK:

C | Python | Java | .NET | Go | node.js

Retrieving via the command line

The cbc tool may be used to fetch documents by their IDs

$ cbc cat docid
docid                CAS=0x8234c3c0f213, Flags=0x0. Size=16
{"json":"value"}

Will output the document’s value and metadata (CAS and flags).

Retrieving JSON documents

Most SDKs will automatically deserialize the JSON document retrieved from the server into your language’s native representation (array, string, dictionary, etc.) depending on the JSON stored.

Retrieving non-JSON documents

The SDK will know what type a document is by inspecting its metadata (See flags). If the document is not JSON (and was not stored as JSON), the SDK will properly deserialize it to the type it was stored as.

Retrieving multiple documents

Multiple documents may be retrieved at once, making better use of the network. See [batching operations] for a discussion of how to efficiently perform multiple operations.

Retrieving and setting expiration

Couchbase Server offers a getAndTouch operation which retrieves an item and updates its expiration time while doing so. This is most useful to prolong the lifetime of transient data, and delay its expiration a bit further. See TTL for more information.

Primary key lookups using N1QL

While N1QL is generally used for secondary queries, it can also be used to retrieve documents by their primary keys (ID) (though it is recommended to use the key-value API if the ID is known). Lookups may be done either by comparing the META(from-term).id or by using the USE KEYS [...] keyword:

SELECT * FROM default USE KEYS ["docid"];

or

SELECT * FROM default WHERE META(default).id = "docid";