Document

    +

    Unresolved include directive in modules/concept-docs/pages/documents.adoc - include::partial$attributes.adoc[]

    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 directive in documents.adoc - include::{version-server}@sdk:shared:partial$documents.adoc[tag=document]

    Primitive Key-Value Operations

    upsert(docid, document)
    insert(docid, document)
    replace(docid, document)
    get(docid)
    remove(docid)

    Unresolved directive in documents.adoc - include::{version-server}@sdk:shared:partial$documents.adoc[tag=crud-overview]

    Unresolved directive in documents.adoc - include::{version-server}@sdk:shared:partial$documents.adoc[tag=store-update]

    If you wish to only modify certain parts of a document, you can use Sub-Document operations which operate on specific subsets of documents:

    val result: Try[MutateInResult] = collection.mutateIn("customer123", Array(
      upsert("email", "dougr96@hotmail.com")
    ))
    
    result match {
      case Success(_)   => println("Success!")
      case Failure(err) => println(s"Error: ${err}")
    }

    or N1QL UPDATE to update documents based on specific query criteria:

    update `default` SET sale_price = msrp * 0.75 WHERE msrp < 19.95;

    Unresolved directive in documents.adoc - include::{version-server}@sdk:shared:partial$documents.adoc[tag=get_generic]

    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

    val result: Try[(String, Boolean)] = for {
      result   <- collection.lookupIn("customer123", Array(
        get("addresses.delivery.country"),
        exists("addresses.delivery.does_not_exist")))
      country  <- result.contentAs[String](0)
      exists   <- result.contentAs[Boolean](1)
    } yield (country, exists)
    
    result match {
      case Success((country, exists)) =>
        println(s"Country = ${country}, Exists = ${exists}}")
      case Failure(err)               =>
        println(s"Error: ${err}")
    }

    Unresolved directive in documents.adoc - include::{version-server}@sdk:shared:partial$documents.adoc[tag=counters1]

    //  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 directive in documents.adoc - include::{version-server}@sdk:shared:partial$documents.adoc[tag=counters2]

    # 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 directive in documents.adoc - include::{version-server}@sdk:shared:partial$documents.adoc[tag=counters3]

    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 directive in documents.adoc - include::{version-server}@sdk:shared:partial$documents.adoc[tag=expiration]