On this page, you can see copyable code examples that show common C++ driver methods for retrieving documents.
TipTo learn more about any of the methods shown on this page, see the link provided in each section.
To use an example from this page, copy the code example into the sample application or your own application. Be sure to replace all placeholders, such as <connection string>
, with the relevant values for your MongoDB deployment.
You can use the following sample application to test the code examples on this page. To use the sample application, perform the following steps:
Ensure you have the C++ driver installed in a location from which your project can import it.
Copy the following code and paste it into a new .cpp
file within your project.
Copy a code example from this page and paste it within the highlighted section of the file.
1#include <iostream>23#include <bsoncxx/builder/basic/document.hpp>4#include <bsoncxx/json.hpp>5#include <mongocxx/client.hpp>6#include <mongocxx/exception/exception.hpp>7#include <mongocxx/instance.hpp>8#include <mongocxx/uri.hpp>910using bsoncxx::builder::basic::kvp;11using bsoncxx::builder::basic::make_document;1213int main() {14 try {15 mongocxx::instance instance;1617 mongocxx::uri uri("<connection string>");18 mongocxx::client client(uri);1920 auto database = client["<database name>"];21 auto collection = database["<collection name>"];2223 2425 2627 } catch (const mongocxx::exception& e) {28 std::cout << "An exception occurred: " << e.what() << "\n";29 return EXIT_FAILURE;30 }31 32 return EXIT_SUCCESS;33}
The following code shows how to retrieve a single document from a collection that matches the specified criteria:
auto result = collection.find_one(make_document(kvp("<field name>", "<value>")));if (result) { std::cout << bsoncxx::to_json(*result) << std::endl;} else { std::cout << "No result found" << std::endl;}
To learn more about the find_one()
method, see the Find One Document section in the Retrieve Data guide.
The following code shows how to retrieve all documents from a collection that match the specified criteria:
auto results = collection.find(make_document(kvp("<field name>", "<value>")));for(auto&& doc : results) { std::cout << bsoncxx::to_json(doc) << std::endl;}
To learn more about the find()
method, see the Find Multiple Documents section in the Retrieve Data guide.
The following code shows how to count the number of documents in a collection:
auto result = collection.count_documents({});std::cout << result << std::endl;
To learn more about the count_documents()
method, see the Retrieve an Accurate Count section in the Count Documents guide.
The following code shows how to count documents in a collection that match the specified criteria:
auto result = collection.count_documents(make_document(kvp("<field name>", "<value>")));std::cout << result << std::endl;
To learn more about the count_documents()
method, see the Retrieve an Accurate Count section in the Count Documents guide.
The following code shows how to retrieve an estimated count of the number of documents in a collection:
auto result = collection.estimated_document_count();std::cout << result << std::endl;
To learn more about the estimated_document_count()
method, see the Retrieve an Estimated Count section in the Count Documents guide.
The following code shows how to retrieve the unique values of a field for documents that match the specified criteria:
auto results = collection.distinct("<field name>", "<filter>");for(auto&& doc : results) { std::cout << bsoncxx::to_json(doc) << std::endl;}
To learn more about the distinct()
method, see the Retrieve Distinct Field Values guide.
The following code shows how to monitor and print changes to a collection:
auto stream = collection.watch();while (true) { for (const auto& event : stream) { std::cout << bsoncxx::to_json(event) << std::endl; }}
To learn more about the watch()
method, see the Monitor Data Changes guide.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4