You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: tutorial/markdown/couchbase-server/best-practices/json-data-modeling-guide/01-comparing-document-oriented-relational-data.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,7 +8,7 @@ description:
8
8
- Learn the different characteristics of each and explore the flexibility of Couchbase
9
9
- See an example dataset get mapped from relational to document-based
Copy file name to clipboardExpand all lines: tutorial/markdown/couchbase-server/best-practices/json-data-modeling-guide/02-using-json-documents.md
+9-5Lines changed: 9 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,7 +7,7 @@ description:
7
7
- A brief introduction to the JSON data format that illustrates the readability and ease of use
8
8
- Learn about the basic data types supported in JSON and view a few example documents
9
9
content_type: tutorial
10
-
filter: n1ql
10
+
filter: sql++
11
11
technology:
12
12
- kv
13
13
- capella
@@ -31,7 +31,9 @@ The following are basic data types supported in JSON:
31
31
32
32
For more information about creating valid JSON documents, please refer to [http://www.json.org](http://www.json.org).
33
33
34
-
When you use JSON documents to represent your application data, you should think about the document as a logical container for information. This involves thinking about how data from your application fits into natural groups. It also requires thinking about the information you want to manage in your application. Doing data modeling for Couchbase Server is a similar process that you would do for traditional relational databases; there is however much more flexibility and you can change your mind later on your data structures. As a best practice, during your data/document design phase, you want to evaluate:
34
+
When you use JSON documents to represent your application data, you should think about the document as a logical container for information. In Couchbase, JSON documents are organized into collections within scopes, which are similar in concept to tables and schemas in relational databases. Collections help isolate types of documents (e.g., `beers`, `breweries`, `users`) within a bucket, improving data organization, access control, and query performance. You should plan your document modeling with collections in mind.
35
+
36
+
This involves thinking about how data from your application fits into natural groups. It also requires thinking about the information you want to manage in your application. Data modeling for Couchbase is a similar process that you would do for traditional relational databases; there is however much more flexibility and you can change your mind later on your data structures. During your data/document design phase, you should evaluate:
35
37
36
38
- What are the _**things**_ you want to manage in your applications, for instance, _users_, _breweries_, _beers_ and so forth.
37
39
- What do you want to store about the _**things**_. For example, this could be _alcohol percentage_, _aroma_, _location_, etc.
@@ -48,7 +50,7 @@ For instance, if you are creating a beer application, you might want a particula
48
50
}
49
51
```
50
52
51
-
For each of the keys in this JSON document you would provide unique values to represent individual beers. If you want to provide more detailed information in your beer application about the actual breweries, you could create a JSON structure to represent a brewery:
53
+
For each of the keys in this JSON document, you would provide unique values to represent individual beers. If you want to provide more detailed information in your beer application about the breweries, you could create a JSON structure to represent a brewery:
52
54
53
55
```json
54
56
{
@@ -61,7 +63,9 @@ For each of the keys in this JSON document you would provide unique values to re
61
63
}
62
64
```
63
65
64
-
Performing data modeling for a document-based application is no different than the work you would need to do for a relational database. For the most part it can be much more flexible, it can provide a more realistic representation or your application data, and it also enables you to change your mind later about data structure. For more complex items in your application, one option is to use nested pairs to represent the information:
66
+
Performing data modeling for a document-based application is no different than the work you would need to do for a relational database. However, it can be much more flexible, it can provide a more realistic representation or your application data, and it also enables you to more easily change your mind later about data structure.
67
+
68
+
For more complex items in your application, one option is to use nested objects to represent the information:
65
69
66
70
```json
67
71
{
@@ -85,6 +89,6 @@ Performing data modeling for a document-based application is no different than t
85
89
}
86
90
```
87
91
88
-
In this case we added a nested attribute for the geolocation of the brewery and for beers. Within the location, we provide an exact longitude and latitude, as well as level of accuracy for plotting it on a map. The level of nesting you provide is your decision; as long as a document is under the maximum storage size for Couchbase Server, you can provide any level of nesting that you can handle in your application.
92
+
In this case we added a nested attribute for the geolocation of the brewery and a nested attribute of its beers. The level of nesting you provide is your decision; as long as a document is under the maximum storage size (20MB per document), you can provide any level of nesting that you can handle in your application.
89
93
90
94
In traditional relational database modeling, you would create tables that contain a subset of information for an item. For instance a _brewery_ may contain types of beers which are stored in a separate table and referenced by the _beer ID_. In the case of JSON documents, you use key-values pairs, or even nested key-value pairs.
Copy file name to clipboardExpand all lines: tutorial/markdown/couchbase-server/best-practices/json-data-modeling-guide/03-schemaless-data-modeling.md
+5-11Lines changed: 5 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,7 +8,7 @@ description:
8
8
- Explore important design considerations to keep in mind when designing JSON documents
9
9
- See how you can use Couchbase's built-in Compare and Swap (CAS) functionality to maintain data consistency
10
10
content_type: tutorial
11
-
filter: n1ql
11
+
filter: sql++
12
12
technology:
13
13
- kv
14
14
- capella
@@ -34,42 +34,38 @@ While you can choose any structure for your documents, the JSON model in particu
34
34
35
35
There are several considerations to have in mind when you design your JSON document:
36
36
37
-
- Whether you want to use a type field at the highest level of your JSON document in order to group and filter object types.
38
37
- What particular keys, ids, prefixes or conventions you want to use for items, for instance **beer\_My\_Brew**.
39
38
- When you want a document to expire, if at all, and what expiration would be best.
40
39
- If want to use a document to access other documents. In other words, you can store keys that refer other documents in a JSON document and get the keys through this document. In the NoSQL database jargon, this is often known as using _composite keys_.
41
40
42
-
You can use a _type_ field to group together sets of records. For example, the following JSON document contains a _type_ field with the value _beer_ to indicate that the document represents a beer. A document that represents another kind of beverage would use a different value in the type field, such as _ale_ or _cider_.
41
+
You can group together sets of records into collections. For example, the following JSON document could be in the "beers" collection. A document that represents another kind of entity would be in a different collection, such as _brewery_ or _user_.
43
42
44
43
```json
45
44
{
46
45
"beer_id": "beer_Hoptimus_Prime",
47
-
"type": "beer",
48
46
"abv": 10,
49
47
"category": "North American Ale",
50
48
"name": "Hoptimus Prime",
51
49
"style": "Double India Pale Ale"
52
50
}
53
51
```
54
52
55
-
Here is another _type_ of document in our application which we use to represent breweries. As in the case of beers, we have a type field we can use now or later to group and categorize our beer producers:
53
+
Here is another document in the _breweries_ collection:
56
54
57
55
```json
58
56
{
59
57
"brewery_id": "brewery_Legacy_Brewing_Co",
60
-
"type": "brewery",
61
58
"name": "Legacy Brewing Co.",
62
59
"address": "525 Canal Street, Reading, Pennsylvania, 19601 United States",
63
60
"updated": "2010-07-22 20:00:20"
64
61
}
65
62
```
66
63
67
-
What happens if we want to change the fields we store for a brewery? In this case we just add the fields to brewery documents. In this case we decide later that we want to include GPS location of the brewery:
64
+
What happens if we want to change the fields we store for a brewery? Add the fields to brewery documents. In this case we decide later that we want to include GPS location of the brewery:
68
65
69
66
```json
70
67
{
71
68
"brewery_id": "brewery_Legacy_Brewing_Co",
72
-
"type": "brewery",
73
69
"name": "Legacy Brewing Co.",
74
70
"address": "525 Canal Street, Reading, Pennsylvania, 19601 United States",
75
71
"updated": "2010-07-22 20:00:20",
@@ -78,7 +74,7 @@ What happens if we want to change the fields we store for a brewery? In this cas
78
74
}
79
75
```
80
76
81
-
So in the case of document-based data, we extend the record by just adding the two new fields for _latitude_ and _longitude_. When we add other breweries after this one, we would include these two new fields. For older breweries we can update them with the new fields or provide programming logic that shows a default for older breweries. The best approach for adding new fields to a document is to perform a compare and swap operation on the document to change it; with this type of operation, Couchbase Server will send you a message that the data has already changed if someone has already changed the record. For more information about compare and swap methods with Couchbase, see [Compare and Swap (CAS)](https://developer.couchbase.com/documentation/server/3.x/developer/dev-guide-3.0/update-info.html#concept29631__cas).
77
+
In the case of document-based data, we extend the record by just adding the two new fields for _latitude_ and _longitude_. When we add other breweries after this one, we would include these two new fields. For older breweries we can update them with the new fields or provide programming logic that shows a default for older breweries. The best approach for adding new fields to a document is to perform a compare and swap operation on the document to change it; with this type of operation, Couchbase will send you a message that the data has already changed if someone has already changed the record. For more information about compare and swap methods with Couchbase, see [Compare and Swap (CAS)](https://docs.couchbase.com/java-sdk/current/howtos/concurrent-document-mutations.html).
82
78
83
79
To create relationships between items, we again use fields. In this example we create a logical connection between beers and breweries using the _brewery_ field in our beer document which relates to the _ID_ field in the brewery document. This is analogous to the idea of using a foreign key in traditional relational database design.
84
80
@@ -87,7 +83,6 @@ This first document represents a beer, Hoptimus Prime:
87
83
```json
88
84
{
89
85
"beer_id": "beer_Hoptimus_Prime",
90
-
"type": "beer",
91
86
"abv": 10,
92
87
"brewery": "brewery_Legacy_Brewing_Co",
93
88
"category": "North American Ale",
@@ -101,7 +96,6 @@ This second document represents the brewery which brews Hoptimus Prime:
101
96
```json
102
97
{
103
98
"brewery_id": "brewery_Legacy_Brewing_Co",
104
-
"type": "brewery",
105
99
"name": "Legacy Brewing Co.",
106
100
"address": "525 Canal Street Reading, Pennsylvania, 19601 United States",
Copy file name to clipboardExpand all lines: tutorial/markdown/couchbase-server/best-practices/json-data-modeling-guide/04-phases-of-data-modeling.md
+11-11Lines changed: 11 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,7 +8,7 @@ description:
8
8
- Compare embedded documents to referenced documents and learn when to use each method
9
9
- See best practices for building your data model
10
10
content_type: tutorial
11
-
filter: n1ql
11
+
filter: sql++
12
12
technology:
13
13
- kv
14
14
- capella
@@ -189,7 +189,6 @@ Referring to canonical instances of documents is a good default when modeling wi
189
189
- You want to ensure your cache is used efficiently.
190
190
- The embedded version would be unwieldy.
191
191
192
-
193
192
That last point is particularly important where your documents have an unbound potential for growth.
194
193
195
194
Imagine we were storing activity logs related to each user of our system. Embedding those logs in the user profile could lead to a rather large document.
@@ -211,17 +210,18 @@ It's unlikely we'd breach Couchbase's 20 MB upper limit for an individual docume
211
210
212
211
The physical data model takes the logical data model and maps the entities and relationships to physical containers.
213
212
214
-
In Couchbase Server, items are used to store associated values that can be accessed with a unique key. Couchbase Server also provides buckets to group items. Based on the access patterns, performance requirements, and atomicity and consistency requirements, you can choose the type of container(s) to use to represent your logical data model.
213
+
In Couchbase, items are used to store associated values that can be accessed with a unique key. Couchbase also provides buckets to group items. Based on the access patterns, performance requirements, and atomicity and consistency requirements, you can choose the type of container(s) to use to represent your logical data model.
215
214
216
-
The data representation and containment in Couchbase Server is drastically different from relational databases. The following table provides a high level comparison to help you get familiar with Couchbase Server containers.
215
+
The data representation and containment in Couchbase is drastically different from relational databases. The following table provides a high level comparison to help you get familiar with Couchbase containers.
217
216
218
-
**Data representation and containment in Couchbase Server versus relational databases:**
217
+
**Data representation and containment in Couchbase versus relational databases:**
| Buckets or Items (with type designator attribute) | Tables |
224
-
| Items (key-value or document) | Rows |
221
+
| Buckets | Databases |
222
+
| Scopes | Schema/namespace |
223
+
| Collections | Tables |
224
+
| Documents/Items | Rows |
225
225
| Index | Index |
226
226
227
227
### Items
@@ -230,8 +230,8 @@ Items consist of a key and a value. A key is a unique identifier within the buck
230
230
231
231
-**Keys**: Each value (binary or JSON) is identified by a unique key. The key is typically a surrogate key generated using a counter or a UUID generator. Keys are immutable. Thus, if you use composite or compound keys, ensure that you use attributes that don't change over time.
232
232
-**Values**
233
-
-**Binary values**: Binary values can be used for high performance access to compact data through keys. Encrypted secrets, IoT instrument measurements, session states, or other non-human-readable data are typical cases for binary data. _Binary_ data may not necessarily be binary, but could be non-JSON formatted text like XML, String, etc. However, using binary values limits the functionality your application can take advantage of, ruling out indexing and querying in Couchbase Server as binary values have a proprietary representation.
234
-
-**JSON values**: JSON provides rich representation for entities. Couchbase Server can parse, index and query JSON values. JSON provide a name and a value for each attribute. You can find the JSON definition at [RFC 7159](https://tools.ietf.org/html/rfc7159) or at [ECMA 404](http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf).
233
+
-**Binary values**: Binary values can be used for high performance access to compact data through keys. Encrypted secrets, IoT instrument measurements, session states, or other non-human-readable data are typical cases for binary data. _Binary_ data may not necessarily be binary, but could be non-JSON formatted text like XML, String, etc. However, using binary values limits the functionality your application can take advantage of, ruling out indexing and querying in Couchbase as binary values have a proprietary representation.
234
+
-**JSON values**: JSON provides rich representation for entities. Couchbase can parse, index and query JSON values. JSON provide a name and a value for each attribute. You can find the JSON definition at [RFC 7159](https://tools.ietf.org/html/rfc7159) or at [ECMA 404](http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf). These type of items are typically called "documents".
235
235
236
236
The JSON document attributes can represent both basic types such as number, string, Boolean, and complex types including embedded documents and arrays. In the examples below, a1 and a2 represent attributes that have a numeric and string value respectively, a3 represents an embedded document, and a4 represents an array of embedded documents.
0 commit comments