From 615bdef068822dbcc69cc2c4b0c84db98a7cf62e Mon Sep 17 00:00:00 2001 From: Taylor McNeil Date: Tue, 28 Jul 2026 18:38:13 -0400 Subject: [PATCH 1/2] Port Get Started for CPP --- README.md | 1 + cpp/hello-world/.gitignore | 1 + cpp/hello-world/CMakeLists.txt | 12 +++++ cpp/hello-world/README.md | 66 +++++++++++++++++++++++++ cpp/hello-world/main.cpp | 88 ++++++++++++++++++++++++++++++++++ 5 files changed, 168 insertions(+) create mode 100644 cpp/hello-world/.gitignore create mode 100644 cpp/hello-world/CMakeLists.txt create mode 100644 cpp/hello-world/README.md create mode 100644 cpp/hello-world/main.cpp diff --git a/README.md b/README.md index 26cf8c5..0255a6f 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,7 @@ includes its own setup instructions. ## Available Client Libraries +- [C++](cpp/hello-world/README.md) - [.NET/C#](dotnet/hello-world/README.md) - [Node.js](node/hello-world/README.md) - [Python](python/hello-world/README.md) diff --git a/cpp/hello-world/.gitignore b/cpp/hello-world/.gitignore new file mode 100644 index 0000000..567609b --- /dev/null +++ b/cpp/hello-world/.gitignore @@ -0,0 +1 @@ +build/ diff --git a/cpp/hello-world/CMakeLists.txt b/cpp/hello-world/CMakeLists.txt new file mode 100644 index 0000000..b5a276c --- /dev/null +++ b/cpp/hello-world/CMakeLists.txt @@ -0,0 +1,12 @@ +cmake_minimum_required(VERSION 3.15) + +project(hello-world CXX) + +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD_REQUIRED ON) + +find_package(mongocxx REQUIRED) + +add_executable(hello-world main.cpp) + +target_link_libraries(hello-world PRIVATE mongo::mongocxx_shared) diff --git a/cpp/hello-world/README.md b/cpp/hello-world/README.md new file mode 100644 index 0000000..6cc8b0f --- /dev/null +++ b/cpp/hello-world/README.md @@ -0,0 +1,66 @@ +# Get Started with the MongoDB C++ Driver + +This sample application connects to a MongoDB deployment, seeds a small +set of sample product documents, and retrieves one of them. Because the +app inserts its own data, you don't need to load an external dataset. + +## Prerequisites + +Before you begin, complete the [Atlas Get Started guide](https://www.mongodb.com/docs/get-started/) +to create a free Atlas deployment and save your database user +credentials. + +You also need the following components installed in your development environment: + +- A C++17-compatible compiler +- CMake 3.15 or later +- The MongoDB C++ driver (mongocxx) 3.10 or later + +## Installation + +Clone this repository: + +```bash +git clone https://github.com/mongodb/mongodb-code-examples +``` + +Install the MongoDB C++ driver (mongocxx). Follow the +[C++ driver installation guide](https://www.mongodb.com/docs/languages/cpp/cpp-driver/current/installation/) +for your platform. + +Navigate into the `cpp/hello-world` project directory and build the +application with CMake: + +```bash +cd mongodb-code-examples/cpp/hello-world +cmake -S . -B build +cmake --build build +``` + +## Connect to MongoDB + +Set your connection string as an environment variable, replacing +`` with your connection string: + +```bash +export MONGODB_URI="" +``` + +## Run the Application + +```bash +./build/hello-world +``` + +When you run the app, it inserts a few product documents into the +`get_started.products` collection, then queries and prints one of them: + +``` +{ "_id" : { "$oid" : "..." }, "name" : "Wireless Mouse", "category" : "Electronics", "price" : 24.99, "tags" : [ "wireless", "usb", "ergonomic" ] } +``` + +You can run the app more than once. It clears the collection before +each run, so the results stay consistent. + +If you encounter an error or see no output, verify that you set the +`MONGODB_URI` environment variable correctly. diff --git a/cpp/hello-world/main.cpp b/cpp/hello-world/main.cpp new file mode 100644 index 0000000..30ff610 --- /dev/null +++ b/cpp/hello-world/main.cpp @@ -0,0 +1,88 @@ +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +using bsoncxx::builder::basic::kvp; +using bsoncxx::builder::basic::make_array; +using bsoncxx::builder::basic::make_document; + +// A few sample product documents seeded by this app so you can run it +// without loading an external dataset. +std::vector sample_products() { + std::vector products; + + products.push_back(make_document( + kvp("name", "Wireless Mouse"), + kvp("category", "Electronics"), + kvp("price", 24.99), + kvp("tags", make_array("wireless", "usb", "ergonomic")))); + + products.push_back(make_document( + kvp("name", "Standing Desk"), + kvp("category", "Furniture"), + kvp("price", 349.99), + kvp("tags", make_array("adjustable", "office")))); + + products.push_back(make_document( + kvp("name", "Noise-Cancelling Headphones"), + kvp("category", "Electronics"), + kvp("price", 199.99), + kvp("tags", make_array("bluetooth", "wireless", "over-ear")))); + + return products; +} + +int main() { + const char* uri_env = std::getenv("MONGODB_URI"); + if (uri_env == nullptr) { + std::cerr << "Set the MONGODB_URI environment variable to your " + "connection string.\n"; + return EXIT_FAILURE; + } + + mongocxx::instance instance{}; + mongocxx::client client{mongocxx::uri{uri_env}}; + + auto database = client["get_started"]; + auto products = database["products"]; + + // Seed the collection so the app has data to query. Clearing the + // collection first keeps results consistent across repeated runs. + products.delete_many({}); + products.insert_many(sample_products()); + + auto filter = make_document(kvp("name", "Wireless Mouse")); + auto product = products.find_one(filter.view()); + if (product) { + auto view = product->view(); + + std::array buf; + auto [ptr, ec] = std::to_chars( + buf.data(), buf.data() + buf.size(), view["price"].get_double().value); + std::string price(buf.data(), ptr); + + std::cout << "{ \"_id\" : { \"$oid\" : \"" + << view["_id"].get_oid().value.to_string() << "\" }" + << ", \"name\" : \"" << view["name"].get_string().value << "\"" + << ", \"category\" : \"" << view["category"].get_string().value + << "\"" + << ", \"price\" : " << price << ", \"tags\" : ["; + bool first = true; + for (auto tag : view["tags"].get_array().value) { + std::cout << (first ? " " : ", ") << "\"" << tag.get_string().value + << "\""; + first = false; + } + std::cout << " ] }\n"; + } + + return EXIT_SUCCESS; +} From b059da23ccc3423ac7512e76dbb1c3ed99ec0c69 Mon Sep 17 00:00:00 2001 From: Taylor McNeil Date: Tue, 28 Jul 2026 18:53:14 -0400 Subject: [PATCH 2/2] updating build --- cpp/hello-world/CMakeLists.txt | 2 +- cpp/hello-world/README.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cpp/hello-world/CMakeLists.txt b/cpp/hello-world/CMakeLists.txt index b5a276c..158d167 100644 --- a/cpp/hello-world/CMakeLists.txt +++ b/cpp/hello-world/CMakeLists.txt @@ -5,7 +5,7 @@ project(hello-world CXX) set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) -find_package(mongocxx REQUIRED) +find_package(mongocxx 4.0 REQUIRED) add_executable(hello-world main.cpp) diff --git a/cpp/hello-world/README.md b/cpp/hello-world/README.md index 6cc8b0f..7025319 100644 --- a/cpp/hello-world/README.md +++ b/cpp/hello-world/README.md @@ -14,7 +14,7 @@ You also need the following components installed in your development environment - A C++17-compatible compiler - CMake 3.15 or later -- The MongoDB C++ driver (mongocxx) 3.10 or later +- The MongoDB C++ driver (mongocxx) 4.0 or later ## Installation