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 kv interface is the fastest way to perform operations involving single documents.
Unresolved include directive in modules/concept-docs/pages/documents.adoc - include::7.0@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::7.0@sdk:shared:partial$documents.adoc[]
Unresolved include directive in modules/concept-docs/pages/documents.adoc - include::7.0@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::7.0@sdk:shared:partial$documents.adoc[]
SELECT * FROM `travel-sample`.inventory.airport USE KEYS ["airport_1254"];
or
SELECT * FROM `travel-sample`.inventory.airport WHERE META().id = "airport_1254";
You can also retrieve parts of documents using sub-document operations, by specifying one or more sections of the document to be retrieved
var res = await collection.get('user:kingarthur', {
project: ['contact.name', 'contact.email']
})
// res.content:
// {
// 'contact': {
// 'name': 'King Arthur',
// 'email': 'theking@knightsoftheroundtable.com',
// }
// }
Unresolved include directive in modules/concept-docs/pages/documents.adoc - include::7.0@sdk:shared:partial$documents.adoc[]
// Java example:
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));
Unresolved include directive in modules/concept-docs/pages/documents.adoc - include::7.0@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::7.0@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::7.0@sdk:shared:partial$documents.adoc[]