Developer Guide Examples
These examples are intended to be linked to from within the developer guide. They are currently cross-referenced by language.
Each example should be 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 but should be be at least hinted at.
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.
This example should show how to store an item into a cluster
This example should show how to get items out of the cluster
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
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.
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 - 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
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.