Key Value Operations

개요

CAS

Docouments

CRUD 작업

CREATE

$document = [
    "foo" => "bar",
    "bar" => "foo"
];
$res = $collection->insert("document-key-new", $document);
printf("document \"document-key\" has been created with CAS \"%s\"\n", $res->cas());

READ

es = $collection->get("document-key");
$doc = $res->content();
printf("document \"document-key\" has content: \"%s\" CAS \"%s\"\n", json_encode($doc), $res->cas());

Expiration / TTL

개요

TouchOption()

$opts = new TouchOptions();
$opts->timeout(500000 /* microseconds */);
$collection->touch($key, 60 /* seconds */);

getAndTouch()

$res = $collection->getAndTouch($key, 1 /* seconds */);
printf("[getAndTouch] document content: %s\n", var_export($res->content(), true));

sleep(2); // wait until the document will expire

try {
    $collection->get($key);
} catch (Couchbase\DocumentNotFoundException $ex) {
    printf("The document does not exist\n");
}

Atomic Counter

INCREMENT

// increment binary value by 1 (default)
$binaryCollection = $collection->binary();
$res = $binaryCollection->increment(<KEY>);

INCREMENT(with options)

$key = "phpDevguideExampleCounter";
$opts = new IncrementOptions();
# Create a document and assign it to 10
$opts = $opts->initial(10);  # if it doesn't exist, counter works atomically by first creating a document
$opts = $opts->delta(2);     # if it exist, the same method will increment/decrement per the "delta" parameter

$res = $binaryCollection->increment($key, $opts);
// Should print 10
printf("Initialized Counter: %d\n", $res->content());

DECREMENT

// decremtnt binary value by 1 (default)
$res = $binaryCollection->decrement("foo");

DECREMENT (with options)

$opts = new DecrementOptions();
$opts->initial(10)->delta(4);
// Decrement value by 4 to 8
$res = $binaryCollection->decrement($key, $opts);
// Should print 8
printf("Decremented Counter: %d\n", $res->content());

하위 문서(Sub-Document) 작업

개요

하위 문서(Sub-Document)

작업

예제 데이터

customer123.json
{
  "name": "Douglas Reynholm",
  "email": "douglas@reynholmindustries.com",
  "addresses": {
    "billing": {
      "line1": "123 Any Street",
      "line2": "Anytown",
      "country": "United Kingdom"
    },
    "delivery": {
      "line1": "123 Any Street",
      "line2": "Anytown",
      "country": "United Kingdom"
    }
  },
  "purchases": {
    "complete": [339, 976, 442, 666],
    "abandoned": [157, 42, 999]
  }
}

lookupIn(\Couchbase\Collection): 조회(Retrieving)

SUBDOC-GET: LookupGetSpec()

$result = $collection->lookupIn("customer123", [
    new \Couchbase\LookupGetSpec("addresses.delivery.country")
]);
$country = $result->content(0);
printf("%s\n", $country);
// "United Kingdom"

SUBDOC-EXISTS: LookupExistsSpec()

$result = $collection->lookupIn("customer123", [
    new \Couchbase\LookupExistsSpec("purchases.pending[-1]")
]);
printf("Path exists? %s\n", $result->exists(0) ? "true" : "false");
// Path exists? false

다중 조회 작업

$result = $collection->lookupIn("customer123", [
    new \Couchbase\LookupGetSpec("addresses.delivery.country"),
    new \Couchbase\LookupExistsSpec("purchases.pending[-1]")
]);
# $result는 \Couchbase\LookupInResult class
printf("%s\n", $result->content(0));
printf("Path exists? %s\n", $result->exists(1) ? "true" : "false");
// United Kingdom
// Path exists? false

mutateIn(\Couchbase\Collection): 수정(Mutating)

SUBDOC-UPSERT: MutateUpsertSpec()

$result = $collection->mutateIn("customer123", [
    new \Couchbase\MutateUpsertSpec("fax", "311-555-0151")
]);

SUBDOC-INSERT: MutateInsertSpec()

$result = $collection->mutateIn("customer123", [
    new \Couchbase\MutateInsertSpec("purchases.complete", [42, true, "None"])
]);
// SubdocPathExistsError

SUBDOC-REMOVE/REPPLACE: MutateRemoveSpec()/MutateReplaceSpec()

$result = $collection->mutateIn("customer123", [
    new \Couchbase\MutateRemoveSpec("addresses.billing"),  // 삭제
    new \Couchbase\MutateReplaceSpec("email", "dougr96@hotmail.com")  // 치환
]);

SUBDOC-ARRAY-APPEND: MutateArrayAppendSpec()

/* BEFORE
"purchases": {
    "complete": [339, 976, 442, 666],
    "abandoned": [157, 42, 999]
}
*/

$result = $collection->mutateIn("customer123", [
    new \Couchbase\MutateArrayAppendSpec("purchases.complete", [777])
]);

/* AFTER
"purchases": {
    "complete": [339, 976, 442, 666, 777*],
    "abandoned": [157, 42, 999]
}
*/

SUBDOC-ARRAY-PREPEND: MutateArrayPrependspec()

/* BEFORE
"purchases": {
    "complete": [339, 976, 442, 666, 777],
    "abandoned": [157, 42, 999]
}
*/

$result = $collection->MutateIn("customer123", [
    new \Couchbase\MutateArrayPrependspec("purchases.abandoned", [18])
]);

/* AFTER
"purchases": {
    "complete": [339, 976, 442, 666, 777],
    "abandoned": [18*, 157, 42, 999]
}
*/

배열만 담는 문서 필요한 경우

$result = $collection->upsert("my_array", []);
$result = $collection->mutateIn("my_array", [
    new \Couchbase\MutateArrayAppendSpec("", ["some element"])
]);
/* the document my_array:
[
    "some element"
]
*/

COLLECTION OF MULTIPLE ELEMENTS

$collection_of_multiple_elements = [
    [
        "element1",
        "element2",
        "element3"
    ]
];

$result = $collection->mutateIn("my_array", [
    new \Couchbase\MutateArrayAppendSpec("", $collection_of_multiple_elements)
]);
/* the document my_array:
[
    "some element",
    [
        "element1",
        "element2",
        "element3"
    ]
]
*/

MULTIPLE ELEMENTS

$multiple_elements = [
    "element1",
    "element2",
    "element3"
];

$result = $collection->mutateIn("my_array", [
    new \Couchbase\MutateArrayAppendSpec("", $multiple_elements)
]);
/* the document my_array:
[
    "some element",
    [
        "element1",
        "element2",
        "element3"
    ],
    "element1",
    "element2",
    "element3"
]
*/