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.
Before you begin, complete the Atlas Get Started guide to create a free Atlas deployment and save your database user credentials.
You also need the following components installed in your development environment:
- CMake 3.15 or later
- A C++17-compatible compiler
- vcpkg (Windows and Linux)
The commands in this guide assume a Bash compatible shell.
Clone this repository:
git clone https://github.com/mongodb/docs-get-startedThis project requires MongoDB C++ driver (mongocxx) 4.0 or later. Install it with your platform's package manager.
macOS / Linux
On macOS, install the MongoDB C++ driver with Homebrew:
brew install mongo-cxx-driverOn Linux, most distribution packages are too old to satisfy this requirement. Use vcpkg instead:
vcpkg install mongo-cxx-driverNavigate into the project directory, then configure and build:
cd docs-get-started/cpp/hello-world
cmake -S . -B build
cmake --build buildIf you installed the driver with vcpkg, add the vcpkg toolchain file to
the configure step so CMake can locate it, replacing <vcpkg root> with
your vcpkg installation path:
cmake -S . -B build -DCMAKE_TOOLCHAIN_FILE=<vcpkg root>/scripts/buildsystems/vcpkg.cmakeWindows
On Windows, use vcpkg to install the MongoDB C++ driver:
vcpkg install mongo-cxx-driverNavigate into the project directory, then configure and build:
cd docs-get-started/cpp/hello-world
cmake -S . -B build -DCMAKE_TOOLCHAIN_FILE=<vcpkg root>/scripts/buildsystems/vcpkg.cmake
cmake --build build --config ReleaseReplace <vcpkg root> with your vcpkg installation path.
For other installation methods, see the C++ driver installation guide.
Set your connection string as an environment variable, replacing
<connection string uri> with your connection string:
export MONGODB_URI="<connection string uri>"On macOS and Linux, run the following command:
./build/hello-worldOn Windows, use the following command instead:
./build/Release/hello-world.exeWhen 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.