|
13 | 13 | <section>
|
14 | 14 | <title>Using Couchbase</title>
|
15 | 15 | <p>Once you've <xref href="../install/install-intro.dita#topic_edn_wtd_54">installed the
|
16 |
| - server</xref>, you can start storing, retrieving, and querying documents with |
| 16 | + server</xref>, and <xref href="../clustersetup/create-bucket.dita">created |
| 17 | + a test bucket</xref>, you can start storing, retrieving, and querying documents with |
17 | 18 | Couchbase. You can start with an SDK, the command-line cbc tool, or the web
|
18 | 19 | browser.</p>
|
19 | 20 | <p>Every item in a database goes through the basic <i>CRUD</i> cycle, which is typical
|
|
40 | 41 | the command line client, don’t worry - we’ll walk through the steps in the next
|
41 | 42 | chapter.</p>
|
42 | 43 | </section>
|
43 |
| - <section><title>Creating documents</title><p>Create the document <i>mnunberg.json</i>. It |
| 44 | + <section><title>Creating documents</title><p>Create the document <i>josmith.json</i>. It |
44 | 45 | can be anywhere on your file
|
45 | 46 | system:</p><codeblock outputclass="language-json">{
|
46 |
| - "name": "Mark Nunberg", |
47 |
| - "email": "mark.nunberg@couchbase.com", |
| 47 | + "name": "Jo Smith", |
| 48 | + "email": "jo.smith@example.com", |
48 | 49 | "privs": ["admin"],
|
49 | 50 | "location": {
|
50 | 51 | "country": "United States",
|
51 | 52 | "state": "NV",
|
52 | 53 | "city": "Reno"
|
53 | 54 | },
|
54 | 55 | "following": [
|
55 |
| - "u:tgreenstein", "u:ingenthr", "u:potus" |
| 56 | + "u:asmith", "u:bsmith", "u:potus" |
56 | 57 | ],
|
57 |
| - "likes": ["doge", "pastries"] |
| 58 | + "likes": ["dogs", "pastries"] |
58 | 59 | }</codeblock><p>Now,
|
59 | 60 | insert the document into Couchbase using the <i><codeph>cbc</codeph></i>
|
60 |
| - utility:</p><screen>$ <b>cbc create -u <varname>username</varname> -P <varname>password</varname> -U couchbase://<varname>hostname-or-ip</varname>/default --mode insert mnunberg < mnunberg.json</b></screen><codeph>mnunberg</codeph> |
| 61 | + utility:</p><screen>$ <b>cbc create -u <varname>username</varname> -P <varname>password</varname> -U couchbase://<varname>hostname-or-ip</varname>/myTestBucket --mode insert josmith < josmith.json</b></screen><codeph>josmith</codeph> |
61 | 62 | is the document’s ID, which is redirected (<codeph><</codeph>) to the
|
62 |
| - <cmdname>cbc</cmdname> command’s standard input.</section> |
| 63 | + <cmdname>cbc</cmdname> command’s standard input.</section> |
63 | 64 | <section>
|
64 | 65 | <title>Reading documents by ID</title>
|
65 | 66 | <p>Documents can be retrieved using their IDs. Retrieving a document by ID is extremely
|
66 | 67 | fast. The following query takes about 1 millisecond.</p>
|
67 |
| - <screen>$ <b>cbc cat -u <varname>username</varname> -P <varname>password</varname> -U couchbase://<varname>hostname-or-ip</varname>/mnunberg</b> |
| 68 | + <screen>$ <b>cbc cat -u <varname>username</varname> -P <varname>password</varname> -U couchbase://<varname>hostname-or-ip</varname>/josmith</b> |
68 | 69 | {
|
69 |
| - "name": "Mark Nunberg", |
70 |
| - "email": "mark.nunberg@couchbase.com", |
| 70 | + "name": "Jo Smith", |
| 71 | + "email": "jo.smith@example.com", |
71 | 72 | "privs": ["admin"],
|
72 | 73 | "location": {
|
73 | 74 | "country": "United States",
|
74 | 75 | "state": "NV",
|
75 | 76 | "city": "Reno"
|
76 | 77 | },
|
77 | 78 | "following": [
|
78 |
| - "u:tgreenstein", "u:ingenthr", "u:potus" |
| 79 | + "u:asmith", "u:bsmith", "u:potus" |
79 | 80 | ],
|
80 |
| - "likes": ["doge", "pastries"] |
| 81 | + "likes": ["dogs", "pastries"] |
81 | 82 | }</screen>
|
82 | 83 | </section>
|
83 | 84 | <section>
|
84 | 85 | <title>Reading documents by querying</title>
|
85 | 86 | <p>Retrieving a document may be done using a unique identifier assigned by the
|
86 | 87 | application at the document’s creation, or by inspecting its contents to see if it
|
87 |
| - matches a certain criteria. ID lookups are quicker, but querying documents allows |
| 88 | + matches a certain criterion. ID lookups are quicker, but querying documents allows |
88 | 89 | for richer search capabilities (for example, "Give me all likes and followed users
|
89 |
| - located in the US?" versus "Give me a user with the ID e3d882a4").</p> |
| 90 | + located in the US" versus "Give me a user with the ID e3d882a4").</p> |
90 | 91 | <screen>$ <b>cbc n1ql \</b>
|
91 | 92 | <b>'SELECT following, likes FROM default WHERE location.country = "United States"'</b>
|
92 | 93 | {
|
93 | 94 | "following": [
|
94 |
| - "u:tgreenstein", |
95 |
| - "u:ingenthr", |
| 95 | + "u:asmith", |
| 96 | + "u:bsmith", |
96 | 97 | "u:potus"
|
97 | 98 | ],
|
98 | 99 | "likes": [
|
99 |
| - "doge", |
| 100 | + "dogs", |
100 | 101 | "pastries"
|
101 | 102 | ]
|
102 | 103 | </screen>
|
103 | 104 | </section>
|
104 | 105 | <section><title>Updating documents</title><p>Updating a document means changing the existing
|
105 | 106 | document. For example, take the file above and edit the <codeph>likes</codeph>
|
106 | 107 | field:</p><screen> ...
|
107 |
| - "likes": ["doge", "pastries", <b>"couchbase"</b>] |
| 108 | + "likes": ["dogs", "pastries", <b>"couchbase"</b>] |
108 | 109 | ....</screen>Then
|
109 | 110 | use the <cmdname>cbc</cmdname> tool to update the document in
|
110 |
| - Couchbase:<screen>$ <b>cbc create --mode replace mnunberg < mnunberg.json</b></screen></section> |
| 111 | + Couchbase:<screen>$ <b>cbc create --mode replace josmith < josmith.json</b></screen></section> |
111 | 112 | <section>
|
112 | 113 | <title>Deleting documents</title>
|
113 | 114 | <p>This example shows how to delete the document with the ID
|
114 |
| - <codeph>mnunberg</codeph>.</p> |
115 |
| - <screen>$ <b>cbc rm mnunberg</b></screen> |
| 115 | + <codeph>josmith</codeph>.</p> |
| 116 | + <screen>$ <b>cbc rm josmith</b></screen> |
116 | 117 | </section>
|
117 | 118 | <section>
|
118 | 119 | <title>Couchbase clients</title>
|
119 | 120 | <p>Clients access data by connecting to a Couchbase cluster over the network. The most
|
120 |
| - common type of client is a Couchbase SDK which is a full programmatic API that |
| 121 | + common type of client is a Couchbase SDK, which is a full programmatic API that |
121 | 122 | enables applications to take the best advantage of Couchbase. The command line
|
122 | 123 | client provides a quick and streamlined interface for simple access and is suitable
|
123 | 124 | if you just want to access an item without writing any code.</p>
|
|
0 commit comments