Getting Started with MongoDB and C++
Rate this tutorial
This article will show you how to utilize Microsoft Visual Studio to compile and install the MongoDB C and C++ drivers on Windows, and use these drivers to create a console application that can interact with your MongoDB data by performing basic CRUD operations.
Tools and libraries used in this tutorial:
- Microsoft Windows 11
- Microsoft Visual Studio 2022 17.3.6
- Language standard: C++17
- MongoDB C Driver version: 1.23
- MongoDB C++ Driver version: 3.7.0
- boost: 1.80.0
- Python: 3.10
- CMake: 3.25.0
- MongoDB Atlas account with a cluster created.
- Your machine’s IP address is whitelisted. Note: You can add 0.0.0.0/0 as the IP address, which should allow access from any machine. This setting is not recommended for production use.
In the Workloads tab during installation, select “Desktop development with C++.”

Step 2: Install CMake: Download | CMake
- For simplicity, choose the installer.
- In the setup, make sure to select “Add CMake to the system PATH for all users.” This enables the CMake executable to be easily accessible.

Detailed instructions and configurations available here:
C++ Driver has a dependency on C driver. Hence, we need to install C Driver first.
- Download C Driver
- Download release tarball — Releases · mongodb/mongo-c-driver — and extract it to C:\Repos\mongo-c-driver-1.23.0.
- Setup build via CMake
- Launch powershell/terminal as an administrator.
- Navigate to C:\Repos\mongo-c-driver-1.23.0 and create a new folder named cmake-build for the build files.
- Navigate to C: \Repos\mongo-c-driver-1.23.0\cmake-build.
- Run the below command to configure and generate build files using CMake.

Note: Build setup can be done with the CMake GUI application, as well.
- Execute build
- Visual Studio’s default build type is Debug. A release build with debug info is recommended for production use.
- Run the below command to build and install the driver

- You should now see libmongoc and libbson installed in C:/Program Files/mongo-c-driver.

- Move the mongo-c-driver to C:/ for convenience. Hence, C Driver should now be present at C:/mongo-c-driver.
- Download C++ Driver
- Download release tarball — Releases · mongodb/mongo-cxx-driver — and extract it to C:\Repos\mongo-cxx-driver-r3.7.0.
- Set up build via CMake
- Launch powershell/terminal as an administrator.
- Navigate to C:\Repos\mongo-cxx-driver-r3.7.0\build.
- Run the below command to generate and configure build files via CMake.

Note: Setting DCMAKE_CXX_FLAGS should not be required for C++ driver version 3.7.1 and above.
- Execute build
- Run the below command to build and install the driver
- You should now see C++ driver installed in C:\mongo-cxx-driver.
- Create a new project in Visual Studio.
- Select Console App in the templates.

- Visual Studio should create a new project and open a .cpp file which prints “Hello World.” Navigate to the Solution Explorer panel, right-click on the solution name (MongoCXXGettingStarted, in this case), and click Properties.

- Go to Configuration Properties > C/C++ > General > Additional Include Directories and add the include directories from the C and C++ driver installation folders, as shown below.

- Go to Configuration Properties > C/C++ > Language and change the C++ Language Standard to C++17.

- Go to Configuration Properties > C/C++ > Command Line and add /Zc:__cplusplus in the Additional Options field. This flag is needed to opt into the correct definition of __cplusplus.
- Go to Configuration Properties > Linker > Input and add the driver libs in Additional Dependencies section, as shown below.

- Go to Configuration Properties > Debugging > Environment to add a path to the driver executables, as shown below.

Source available here
Let’s build an application that maintains student records. We will input student data from the user, save them in the database, and perform different CRUD operations on the database.
Let’s start with a simple program to connect to the MongoDB Atlas cluster and access the databases. Get the connection string (URI) to the cluster and create a new environment variable with key as “MONGODB_URI” and value as the connection string (URI). It’s a good practice to keep the connection string decoupled from the code.
Tip: Restart your machine after creating the environment variable in case the “getEnvironmentVariable” function fails to retrieve the environment variable.
Click on “Launch Debugger” to launch the console application. The output should looks something like this:

Since the database is successfully connected to our application, let’s write some helper functions to interact with the database, performing CRUD operations.
With all the helper functions in place, let’s create a menu in the main function which we can use to interact with the application.
When this application is executed, you can manage the student records via the console interface. Here’s a demo:
You can also see the collection in Atlas reflecting any change made via the console application.

With this article, we covered installation of C/C++ driver and creating a console application in Visual Studio that connects to MongoDB Atlas to perform basic CRUD operations.