Document
Couchbase supports CRUD operations, various data structures, and binary documents.
Although query and path-based (Sub-Document) services are available, the simplicity of the document-based key-value (kv) interface is the fastest way to perform operations involving single documents.
Unresolved include directive in modules/concept-docs/pages/documents.adoc - include::6.5@sdk:shared:partial$documents.adoc[]
Primitive Key-Value Operations
upsert(docid, document)
insert(docid, document)
replace(docid, document)
get(docid)
remove(docid)
Unresolved include directive in modules/concept-docs/pages/documents.adoc - include::6.5@sdk:shared:partial$documents.adoc[]
Unresolved include directive in modules/concept-docs/pages/documents.adoc - include::6.5@sdk:shared:partial$documents.adoc[]
If you wish to only modify certain parts of a document, you can use sub-document operations which operate on specific subsets of documents:
or N1QL UPDATE to update documents based on specific query criteria:
|
Unresolved include directive in modules/concept-docs/pages/documents.adoc - include::6.5@sdk:shared:partial$documents.adoc[]
SELECT * FROM default USE KEYS ["docid"];
or
SELECT * FROM default WHERE META(default).id = "docid";
You can also retrieve parts of documents using sub-document operations, by specifying one or more sections of the document to be retrieved
name, email = cb.retrieve_in('user:kingarthur', 'contact.name', 'contact.email')
Unresolved include directive in modules/concept-docs/pages/documents.adoc - include::6.5@sdk:shared:partial$documents.adoc[]
String counterDocId = "counter-doc";
// Increment by 1, creating doc if needed
collection.binary().increment(counterDocId);
// Decrement by 1
collection.binary().decrement(counterDocId);
// Decrement by 5
collection.binary().decrement(counterDocId,
DecrementOptions.decrementOptions().delta(5));
You can simplify by importing decrementOptions()
statically:
collection.binary().decrement(counterDocId, decrementOptions().delta(5));
Unresolved include directive in modules/concept-docs/pages/documents.adoc - include::6.5@sdk:shared:partial$documents.adoc[]
# Python example:
rv = cb.get('counter_id')
value, cas = rv.value, rv.cas
if should_increment_value(value):
cb.upsert('counter_id', value + increment_amount, cas=cas)
Unresolved include directive in modules/concept-docs/pages/documents.adoc - include::6.5@sdk:shared:partial$documents.adoc[]
Use Cases
The SDK provides a high-level abstraction over the simple incr()
/decr()
of Couchbase Server’s memcached binary protocol, using collections.binary()
.
This enables you to work with counters using get()
and upsert()
operations — allowing, inter alia, the use of durability options with the operations.
You will find several ways of working with counters in the API docs.
Unresolved include directive in modules/concept-docs/pages/documents.adoc - include::6.5@sdk:shared:partial$documents.adoc[]