Skip to content

Commit b0a7f7a

Browse files
committed
DOCSP-49686: Check for no results before printing
1 parent 7316fc0 commit b0a7f7a

File tree

4 files changed

+18
-7
lines changed

4 files changed

+18
-7
lines changed

source/get-started.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -306,9 +306,9 @@ Run a Sample Query
306306
"title" : "The Shawshank Redemption",
307307
...
308308

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.
309+
If you encounter an error or if your application prints ``"No result found"``,
310+
ensure that you specified the proper connection string in the ``quickstart.cpp``
311+
file and that you loaded the sample data.
312312

313313
After you complete these steps, you have a working application that
314314
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)