Skip to content

Commit e974be2

Browse files
authored
DOCSP-49686: Check for no results before printing (#132)
* DOCSP-49686: Check for no results before printing * fix
1 parent 7316fc0 commit e974be2

File tree

4 files changed

+20
-7
lines changed

4 files changed

+20
-7
lines changed

source/get-started.txt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,8 @@ Run a Sample Query
260260
the ``movies`` collection in the ``sample_mflix`` database:
261261

262262
.. literalinclude:: /includes/get-started/quickstart.cpp
263+
:language: cpp
264+
:dedent:
263265

264266
.. step:: Assign the connection string
265267

@@ -306,9 +308,9 @@ Run a Sample Query
306308
"title" : "The Shawshank Redemption",
307309
...
308310

309-
If you encounter an error or see no output, ensure that you specified the
310-
proper connection string in the ``quickstart.cpp`` file and that you loaded the
311-
sample data.
311+
If you encounter an error or if your application prints ``"No result found"``,
312+
ensure that you specified the proper connection string in the ``quickstart.cpp``
313+
file and that you loaded the sample data.
312314

313315
After you complete these steps, you have a working application that
314316
uses the driver to connect to your MongoDB deployment, runs a query on

source/includes/get-started/quickstart.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ int main() {
1919
auto collection = db["movies"];
2020

2121
auto result = collection.find_one(make_document(kvp("title", "The Shawshank Redemption")));
22-
std::cout << bsoncxx::to_json(*result) << std::endl;
23-
22+
if (result) {
23+
std::cout << bsoncxx::to_json(*result) << std::endl;
24+
} else {
25+
std::cout << "No result found" << std::endl;
26+
}
2427
}

source/includes/read/retrieve.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ int main() {
2525
// Retrieves and prints one document that has a "name" value of "LinkedIn"
2626
// start-find-one
2727
auto result = collection.find_one(make_document(kvp("name", "LinkedIn")));
28-
std::cout << bsoncxx::to_json(*result) << std::endl;
28+
if (result) {
29+
std::cout << bsoncxx::to_json(*result) << std::endl;
30+
} else {
31+
std::cout << "No result found" << std::endl;
32+
}
2933
// end-find-one
3034

3135
// Retrieves documents that have a "founded_year" value of 1970

source/includes/usage-examples/read-code-examples.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ int main() {
2323
// Retrieves one document that matches a query filter
2424
// start-find-one
2525
auto result = collection.find_one(make_document(kvp("<field name>", "<value>")));
26-
std::cout << bsoncxx::to_json(*result) << std::endl;
26+
if (result) {
27+
std::cout << bsoncxx::to_json(*result) << std::endl;
28+
} else {
29+
std::cout << "No result found" << std::endl;
30+
}
2731
// end-find-one
2832
}
2933

0 commit comments

Comments
 (0)