Skip to content

Latest commit

 

History

History
124 lines (85 loc) · 3.21 KB

File metadata and controls

124 lines (85 loc) · 3.21 KB

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 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.

Installation

Clone this repository:

git clone https://github.com/mongodb/docs-get-started

Install the MongoDB C++ Driver

This 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-driver

On Linux, most distribution packages are too old to satisfy this requirement. Use vcpkg instead:

vcpkg install mongo-cxx-driver

Navigate into the project directory, then configure and build:

cd docs-get-started/cpp/hello-world
cmake -S . -B build
cmake --build build

If 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.cmake
Windows

On Windows, use vcpkg to install the MongoDB C++ driver:

vcpkg install mongo-cxx-driver

Navigate 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 Release

Replace <vcpkg root> with your vcpkg installation path.

For other installation methods, see the C++ driver installation guide.

Connect to MongoDB

Set your connection string as an environment variable, replacing <connection string uri> with your connection string:

export MONGODB_URI="<connection string uri>"

Run the Application

On macOS and Linux, run the following command:

./build/hello-world

On Windows, use the following command instead:

./build/Release/hello-world.exe

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.