EventGet 50% off your ticket to MongoDB.local NYC on May 2. Use code Web50!Learn more >>
MongoDB Developer
MongoDB
plus
Sign in to follow topics
MongoDB Developer Centerchevron-right
Developer Topicschevron-right
Productschevron-right
MongoDBchevron-right

Getting Started with MongoDB and C++

Rishabh Bisht7 min read • Published Jan 19, 2023 • Updated Aug 03, 2023
MongoDBC++
Facebook Icontwitter iconlinkedin icon
Rate this tutorial
star-empty
star-empty
star-empty
star-empty
star-empty
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:
  1. Microsoft Windows 11
  2. Microsoft Visual Studio 2022 17.3.6
  3. Language standard: C++17
  4. MongoDB C Driver version: 1.23
  5. MongoDB C++ Driver version: 3.7.0
  6. boost: 1.80.0
  7. Python: 3.10
  8. CMake: 3.25.0

Prerequisites

  1. MongoDB Atlas account with a cluster created.
  2. (Optional) Sample dataset loaded into the Atlas cluster.
  3. 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.

Installation: IDE and tools

In the Workloads tab during installation, select “Desktop development with C++.”
Microsoft Visual Studio installation
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.
Install options for CMake
Step 3: Install Python 3: Download Python.
Step 4: (Optional) Download boost library from Boost Downloads and extract it to C:\boost.

Installation: Drivers

Detailed instructions and configurations available here:

Step 1: Install C Driver

C++ Driver has a dependency on C driver. Hence, we need to install C Driver first.
  • Download C Driver
  • 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.
Setting up MongoDB C driver build via CMake and Microsoft Visual Studio
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
Building MongoDB C driver via CMake and Microsoft Visual Studio
  • You should now see libmongoc and libbson installed in C:/Program Files/mongo-c-driver.
Building MongoDB C driver via CMake and Microsoft Visual Studio
  • Move the mongo-c-driver to C:/ for convenience. Hence, C Driver should now be present at C:/mongo-c-driver.

Step 2: Install C++ Driver

  • Download C++ Driver
  • 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.
Setting up MongoDB C++ driver build via CMake and Microsoft Visual Studio
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.

Visual Studio: Setting up the dev environment

  • Create a new project in Visual Studio.
  • Select Console App in the templates.
New project options in Microsoft Visual Studio
  • 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.
Microsoft Visual Studio solution 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.
Microsoft Visual Studio include directories
  • Go to Configuration Properties > C/C++ > Language and change the C++ Language Standard to C++17.
Microsoft Visual Studio C++ language standard
  • 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.
Microsoft Visual Studio linker dependencies
  • Go to Configuration Properties > Debugging > Environment to add a path to the driver executables, as shown below.
Microsoft Visual Studio debugging environment

Building the console application

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.

Connecting to 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:
Microsoft Visual Studio debug console

CRUD operations

Since the database is successfully connected to our application, let’s write some helper functions to interact with the database, performing CRUD operations.

Create

Read

Update

Delete

The main() function

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.

Application in action

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.
Student Records collection in Atlas

Wrapping up

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.
More information about the C++ driver is available at MongoDB C++ Driver.

Facebook Icontwitter iconlinkedin icon
Rate this tutorial
star-empty
star-empty
star-empty
star-empty
star-empty
Related
Tutorial

MongoDB Aggregation Pipeline Queries vs SQL Queries


May 10, 2022 | 7 min read
Article

Paginations 2.0: Why I Would Choose MongoDB


May 19, 2022 | 4 min read
News & Announcements

Improved Error Messages for Schema Validation in MongoDB 5.0


Jun 14, 2023 | 10 min read
Quickstart

Java - Client Side Field Level Encryption


Mar 01, 2024 | 14 min read
Table of Contents