Skip to content

Commit c20f2f2

Browse files
Merge pull request #359 from couchbase/subdoc-polish
Polish Sub-Document examples
2 parents b891da1 + 9ed0c43 commit c20f2f2

File tree

2 files changed

+24
-20
lines changed

2 files changed

+24
-20
lines changed

modules/howtos/examples/SubDocument.java

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -96,33 +96,41 @@ public static void main(String... args) {
9696

9797
static void getFunc() {
9898
// tag::get[]
99-
LookupInResult result = collection.lookupIn("hotel_1368", Collections.singletonList(get("geo.lat")));
99+
LookupInResult result = collection.lookupIn("hotel_1368",
100+
Collections.singletonList(get("geo.lat")));
100101

101-
String str = result.contentAs(0, String.class);
102-
System.out.println("getFunc: Latitude = " + str);
102+
try {
103+
String str = result.contentAs(0, String.class);
104+
System.out.println("getFunc: Latitude = " + str);
105+
} catch (PathNotFoundException e) {
106+
e.printStackTrace();
107+
}
103108
// end::get[]
104109
}
105110

106111
static void existsFunc() {
107-
108112
// tag::exists[]
109-
try {
110-
LookupInResult result = collection.lookupIn("hotel_1368",
111-
Collections.singletonList(exists("address.does_not_exist")));
112-
} catch (PathNotFoundException e) {
113-
System.out.println("existsFunc: " + e);
114-
}
113+
LookupInResult result = collection.lookupIn("hotel_1368",
114+
Collections.singletonList(exists("address.does_not_exist")));
115+
boolean pathExists = result.exists(0);
116+
System.out.println("Non-existent path exists? " + pathExists);
115117
// end::exists[]
116118
}
117119

118120
static void combine() {
119121
// tag::combine[]
120-
try {
121-
LookupInResult result = collection.lookupIn("hotel_1368",
122-
Arrays.asList(get("geo.lat"), exists("address.does_not_exist")));
123-
} catch (PathNotFoundException e) {
124-
System.out.println("combine: " + e);
125-
}
122+
LookupInResult result = collection.lookupIn("hotel_1368",
123+
Arrays.asList(
124+
get("geo.lat"), // index 0
125+
exists("address.does_not_exist") // index 1
126+
)
127+
);
128+
129+
String lat = result.contentAs(0, String.class);
130+
boolean otherExists = result.exists(1);
131+
132+
System.out.println("Latitude: " + lat);
133+
System.out.println("Non-existent path exists? " + otherExists);
126134
// end::combine[]
127135
}
128136

modules/howtos/pages/subdocument-operations.adoc

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,6 @@ NOTE: The operation used here is `LookupInSpec.get`, but we import this static m
9696
include::example$SubDocument.java[tag=exists,indent=0]
9797
----
9898

99-
NOTE: LookupInResult has an `exists` method, but this should not be confused with the exists __operation__. The `exists` method
100-
is used to check if anything was returned by the server for a given operation. The result of the exists operation should
101-
be checked with `contentAs(X, Boolean.class)`, as in the example.
102-
10399
Multiple operations can be combined:
104100

105101
.Combine multiple lookup operations

0 commit comments

Comments
 (0)