Skip to content

Commit e03697d

Browse files
Merge pull request #2 from mongodb-developer/fix/java-null-messages
changing the way to print messages to fix empty collection issue
2 parents 187b245 + 5346b8d commit e03697d

5 files changed

Lines changed: 24 additions & 95 deletions

File tree

java/10_find.ipynb

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -169,15 +169,9 @@
169169
"outputs": [],
170170
"source": [
171171
"Bson booksAfter2008 = gt(\"year\", 2008);\n",
172-
"FindIterable<Document> cursor = books.find(booksAfter2008);\n",
173172
"\n",
174-
"if (cursor != null) {\n",
175-
" for(Document b: cursor) {\n",
176-
" System.out.println(\"Book: \" + b.toJson());\n",
177-
" }\n",
178-
"} else {\n",
179-
" System.out.println(\"Empty collection\");\n",
180-
"}"
173+
"books.find(booksAfter2008)\n",
174+
" .forEach(b -> System.out.println(\"Book: \" + b.toJson())); "
181175
]
182176
},
183177
{
@@ -202,15 +196,9 @@
202196
"Bson booksWithLessThan50pages = lt(\"pages\", 50);\n",
203197
"Bson showOnlyTitleAndPages = Projections.fields(Projections.include(\"title\", \"pages\"));\n",
204198
"\n",
205-
"FindIterable<Document> cursor = books.find(booksWithLessThan50pages).projection(showOnlyTitleAndPages);\n",
206-
"\n",
207-
"if (cursor != null) {\n",
208-
" for(Document b: cursor) {\n",
209-
" System.out.println(\"Book: \" + b.toJson());\n",
210-
" }\n",
211-
"} else {\n",
212-
" System.out.println(\"Empty collection\");\n",
213-
"}"
199+
"books.find(booksWithLessThan50pages)\n",
200+
" .projection(showOnlyTitleAndPages)\n",
201+
" .forEach(b -> System.out.println(\"Book: \" + b.toJson()));"
214202
]
215203
},
216204
{
@@ -238,15 +226,9 @@
238226
" Projections.excludeId()\n",
239227
");\n",
240228
"\n",
241-
"FindIterable<Document> cursor = books.find(booksWithLessThan50pages).projection(showOnlyTitleAndPagesNoId);\n",
242-
"\n",
243-
"if (cursor != null) {\n",
244-
" for(Document b: cursor) {\n",
245-
" System.out.println(\"Book: \" + b.toJson());\n",
246-
" }\n",
247-
"} else {\n",
248-
" System.out.println(\"Empty collection\");\n",
249-
"}"
229+
"books.find(booksWithLessThan50pages)\n",
230+
" .projection(showOnlyTitleAndPagesNoId)\n",
231+
" .forEach(b -> System.out.println(\"Book: \" + b.toJson())); "
250232
]
251233
},
252234
{

java/11_find_arrays.ipynb

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,8 @@
9292
"outputs": [],
9393
"source": [
9494
"Bson poetryBooks = eq(\"genres\", \"Poetry\");\n",
95-
"FindIterable<Document> cursor = books.find(poetryBooks);\n",
96-
"\n",
97-
"if (cursor != null) {\n",
98-
" for(Document b: cursor) {\n",
99-
" System.out.println(\"Book: \" + b.toJson());\n",
100-
" }\n",
101-
"} else {\n",
102-
" System.out.println(\"Empty collection\");\n",
103-
"}"
95+
"books.find(poetryBooks)\n",
96+
" .forEach(b -> System.out.println(\"Book: \" + b.toJson()));"
10497
]
10598
},
10699
{
@@ -123,15 +116,9 @@
123116
"outputs": [],
124117
"source": [
125118
"Bson familyLifeAndFictionBooks = all(\"genres\", \"Family Life\", \"Fiction\");\n",
126-
"FindIterable<Document> cursor = books.find(familyLifeAndFictionBooks);\n",
127119
"\n",
128-
"if (cursor != null) {\n",
129-
" for(Document b: cursor) {\n",
130-
" System.out.println(\"Book: \" + b.toJson());\n",
131-
" }\n",
132-
"} else {\n",
133-
" System.out.println(\"Empty collection\");\n",
134-
"}"
120+
"books.find(familyLifeAndFictionBooks)\n",
121+
" .forEach(b -> System.out.println(\"Book: \" + b.toJson()));"
135122
]
136123
},
137124
{
@@ -154,15 +141,9 @@
154141
"outputs": [],
155142
"source": [
156143
"Bson familyLifeOrFictionBooks = in(\"genres\", \"Family Life\", \"Fiction\");\n",
157-
"FindIterable<Document> cursor = books.find(familyLifeOrFictionBooks);\n",
158144
"\n",
159-
"if (cursor != null) {\n",
160-
" for(Document b: cursor) {\n",
161-
" System.out.println(\"Book: \" + b.toJson());\n",
162-
" }\n",
163-
"} else {\n",
164-
" System.out.println(\"Empty collection\");\n",
165-
"}"
145+
"books.find(familyLifeOrFictionBooks)\n",
146+
" .forEach(b -> System.out.println(\"Book: \" + b.toJson()));"
166147
]
167148
},
168149
{
@@ -187,15 +168,9 @@
187168
"outputs": [],
188169
"source": [
189170
"Bson poetryAndFictionBooks = eq(\"genres\", Arrays.asList(\"Poetry\", \"Fiction\"));\n",
190-
"FindIterable<Document> cursor = books.find(poetryAndFictionBooks);\n",
191171
"\n",
192-
"if (cursor != null) {\n",
193-
" for(Document b: cursor) {\n",
194-
" System.out.println(\"Book: \" + b.toJson());\n",
195-
" }\n",
196-
"} else {\n",
197-
" System.out.println(\"Empty collection\");\n",
198-
"}"
172+
"books.find(poetryAndFictionBooks)\n",
173+
" .forEach(b -> System.out.println(\"Book: \" + b.toJson()));"
199174
]
200175
}
201176
],

java/30_insert.ipynb

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -121,15 +121,8 @@
121121
"source": [
122122
"Document bookFrom1500 = new Document(\"year\", 1500);\n",
123123
"\n",
124-
"FindIterable<Document> cursor = books.find(bookFrom1500);\n",
125-
"\n",
126-
"if (cursor != null) {\n",
127-
" for(Document b: cursor) {\n",
128-
" System.out.println(\"Book: \" + b.toJson());\n",
129-
" }\n",
130-
"} else {\n",
131-
" System.out.println(\"Empty collection\");\n",
132-
"}"
124+
"books.find(bookFrom1500)\n",
125+
" .forEach(b -> System.out.println(\"Book: \" + b.toJson()));"
133126
]
134127
},
135128
{
@@ -153,15 +146,8 @@
153146
"source": [
154147
"Document bookFrom1500 = new Document(\"_id\", new ObjectId(\"\")); // add the _id here!\n",
155148
"\n",
156-
"FindIterable<Document> cursor = books.find(bookFrom1500);\n",
157-
"\n",
158-
"if (cursor != null) {\n",
159-
" for(Document b: cursor) {\n",
160-
" System.out.println(\"Book: \" + b.toJson());\n",
161-
" }\n",
162-
"} else {\n",
163-
" System.out.println(\"Empty collection\");\n",
164-
"}"
149+
"books.find(bookFrom1500)\n",
150+
" .forEach(b -> System.out.println(\"Book: \" + b.toJson()));"
165151
]
166152
},
167153
{

java/40_delete.ipynb

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -121,15 +121,8 @@
121121
"source": [
122122
"Document bookFrom1500 = new Document(\"_id\", \"platero\"); \n",
123123
"\n",
124-
"FindIterable<Document> cursor = books.find(bookFrom1500);\n",
125-
"\n",
126-
"if (cursor != null) {\n",
127-
" for(Document b: cursor) {\n",
128-
" System.out.println(\"Book: \" + b.toJson());\n",
129-
" }\n",
130-
"} else {\n",
131-
" System.out.println(\"Empty collection\");\n",
132-
"}"
124+
"books.find(bookFrom1500)\n",
125+
" .forEach(b -> System.out.println(\"Book: \" + b.toJson())); "
133126
]
134127
},
135128
{

java/50_update.ipynb

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -121,15 +121,8 @@
121121
"source": [
122122
"Document elQuijote = new Document(\"_id\", \"quijote\"); \n",
123123
"\n",
124-
"FindIterable<Document> cursor = books.find(elQuijote);\n",
125-
"\n",
126-
"if (cursor != null) {\n",
127-
" for(Document b: cursor) {\n",
128-
" System.out.println(\"Book: \" + b.toJson());\n",
129-
" }\n",
130-
"} else {\n",
131-
" System.out.println(\"Empty collection\");\n",
132-
"}"
124+
"books.find(elQuijote)\n",
125+
" .forEach(b -> System.out.println(\"Book: \" + b.toJson())); "
133126
]
134127
},
135128
{

0 commit comments

Comments
 (0)