Developer Guide Examples
These examples are the basis for examples published in Couchbase documentation. There are examples for each language.
By design each example is fully self-contained and executable. For languages which have a significant setup, the example may be split into multiple files, but should still be executable. Error handling is optional to keep the example easy to read but should be described.
How to Use These Examples
The examples are versioned by the features introduced in a given server version.
server-4.5 branch should be used for documentation content related
to Couchbase Server 4.5, and so on.
- Connecting to Couchbase
- Basic Document/KV Operations
- Advanced Document/KV Operations
- Sub-Document Operations
- N1QL Queries
- Field Encryption
Connecting to Couchbase
These examples show how to establish a connection from an SDK to a Couchbase cluster.
This example should show how to connect to a remote Couchbase cluster and bucket
This example shows how to connect using the server's SSL certificate which has already been downloaded locally to the SDK host. The C-based examples will refer to this path when connecting, while the other SDKs will refer to a specific store and possibly have the example divided into installing and using the certificate.
Basic Document/KV Operations
These examples demonstrate the most basic functionality of working with documents.
These examples show how to store an item into a cluster
This example should show how to get items out of the cluster
Advanced Document/KV Operations
These examples show how to perform more detailed operations on documents
This example should show how to store items into a cluster using the Bulk API
This example should show how to get items out of the cluster using the Bulk API
This example should show how to initialize and update a counter
This example should show how to initialize a document with an Expiry or "ttl" - time to live
CAS Handling - Using CAS for Concurrent Mutations
This example will demonstrate concurrent mutations with and without using the CAS value. Without using the CAS value, some modifications may end up getting lost, whereas using the CAS within a proper retry mechanism will ensure that all mutations remain in tact
Shows storing an item with durability requirements, attempting to persist/replicate to the maximum number of nodes available.
Some SDKs provide APIs to determine how many nodes are in the cluster, while some SDKs allow dynamically persisting to the total number of nodes available, while others only allow fixed numbers. More details may be found in the examples.
Sub-Document operations (new in Couchbase 4.5) allows efficient addressing of sections within documents (sub-documents). Sub-Document is often abbreviated as subdoc.
Subdoc - Retrieving
Retrieve a few fields from a document; also demonstrate error handling if some fields are missing
Subdoc - Updating/Storing
Modify a few fields within a document. Show error handling and the behavior of the
These examples show how to query using N1QL
Query with Criteria
This example should show how to perform a simple query against the travel-sample bucket. The query is something like:
query = N1QLQuery('SELECT airportname, city, country FROM `travel-sample` ' 'WHERE type="airport" AND city="Reno"')
Query with Placeholders
This example should demonstrate how to use placeholders, and also the advantages they afford, perhaps by abstracting a given query away as a function, and passing a function parameter down as a query parameter. Note: the term "placeholder" was chosen as the term "parameter" is overloaded. Placeholder is a bit more related to what is happening in a query statement.
This example should also demonstrate in a commented section of code how to optimize this query.
Query - Ensuring all Documents are the Latest (scan consistency)
This example should show how the
scan_consistency parameter may be enabled for a specific query.
Query - Ensuring all documents are consistent with a given mutation (
This example should show performing a mutation, and then showing a query that is consistent with that given mutation
Query - Better Reuse of Queries with adhoc(false) and Prepared Statements
This example should demonstrate best practice when a statement is to be reused heavily. Setting the
adhoc N1QL query tuning to
false will use Prepared Statements in the background, which is useful in such a case. Note how this works with placeholders (but of course simple statements work too).
C | Python | Java | .NET | Go | node.js
Query - UPDATE and DELETE
Show how these statements can be used to modify existing documents using
secondary document attributes. Also show how a single document can be
modified via the
USE KEYS clause.
C | Python | .NET | Go | node.js
Full-Text (CBFT) Queries
Basic full-text example
This should show basic functionality of the FTS feature. Search for 'hoppy'
This should show a basic example of creating and removing a bucket. It might also be good to show how to wait until a newly created bucket becomes ready as well.
Flushing a Bucket
This should show an example of how to flush a bucket.
Creating N1QL Primary Index
This should show how to create the primary N1QL index on a bucket, ignoring whether it exists or not
Symmetric Key Encryption (AES)
An example of Symmetric Key encryption using AES-256 and SHA256 signing to encrypt fields within a document.
Asymmetric Key Encryption (RSA)
An example of field field encryption using the Asymmetric Key RSA-2048 algorithm and OAEP-SHA1 padding.
Development and Branching
As mentioned above, this repository has a branch per server release. The choice of a branch (versus a tag) is intentional, as samples may continue to be added after a release. This is more documentation management than software management.
In general, if adding samples for the version under development, add them to the 'master' branch. At some point of completion, this will be branched when released.
If adding a sample to an 'older' branch, please also cherry-pick it to master or other relevant branches. For instance, if one were to add a sample to server-4.6, it should also be cherry-picked to master, server-5.0, server-5.1, server-5.5. Better yet might be to add it to master and then cherry-pick it to the relevant releases.