BlogAtlas Vector Search voted most loved vector database in 2024 Retool State of AI report Read more >

C++ and MongoDB

C++ stands as a robust, high-performance programming language, acclaimed for its efficiency, versatility and widespread usage. Given the popularity of both MongoDB and C++, it is no surprise that they work well together. Whether you are developing on a local or on-premise MongoDB installation, or going cloud-first with MongoDB Atlas, the MongoDB C++ Driver makes it a breeze to integrate MongoDB into your development process.

Are you trying to find out which database to use with C++ application you are developing? Dive into this guide to explore the seamless integration of C++ with MongoDB, harnessing the strengths of C++ alongside the flexibility and scalability of MongoDB’s database system.

Table of contents

Why use MongoDB with C++

MongoDB holds the top spot as the most popular NoSQL database technology. It is a document database used to build highly available and scalable applications. Founded in 2007, MongoDB has a worldwide following in the developer community. MongoDB has always focused on providing developers with an excellent user experience, which, in addition to all its other features, has made MongoDB a favorite of developers worldwide. C++ developers can benefit significantly from using MongoDB for several reasons:

  • Flexible Schema: Unlike SQL, MongoDB’s document model allows for different documents in a collection to have diverse structured data. This flexibility aligns well with C++’s dynamic nature and can increase productivity by allowing changes in data layout without having to modify your entire data model.
  • Performance: MongoDB has built-in support for sharding and replication, offering high availability, horizontal scaling, and geographic distribution. This makes it an ideal choice for C++ applications that require robustness and high performance.
  • Scalability: MongoDB is designed for easy horizontal scaling, which involves adding more servers for your application. In contrast, SQL databases typically rely on vertical scaling (more powerful hardware), which can present limitations.
  • Ease of Use: MongoDB provides a very straightforward and intuitive API for CRUD operations, which can be easily used with the MongoDB C++ Driver. JSON-like documents used in MongoDB map directly to data types in C++, providing a natural data manipulation environment.

If you’re building something new and need a database, consider using MongoDB Atlas from the outset. Atlas will give you a fully-managed, cloud-native database service that comes with a number of features, including full-text search, vector search, charts, partner integrations, and much more.

Fun Fact: MongoDB database is written in C++ language!

Getting started with MongoDB and C++

To start using MongoDB with your C++ application, you would need to install the MongoDB C++ driver, which acts as the interface between MongoDB and your C++ application. The driver is also known as mongocxx or mongo-cxx-driver.

There are multiple options available to install the MongoDB C++ driver:

See the detailed documentation on the installation page that covers all of the above methods. A step by step guide for building and installing from source on Windows is also available. We also have a 10 minute video tutorial to get you started.

You can set up MongoDB C++ driver with the IDE of your choice, such as Visual Studio, Visual Studio Code or CLion. The C++ driver installation also includes a CMake config-file package, so you can use CMake’s find_package command to import C++ driver’s CMake target and link to the C++ driver (as a shared library).

Creating your first Atlas cluster

Once the C++ driver is all set up, you’ll need a database.

You can create an account and create a free-tier cluster on MongoDB Atlas by following this guide. It will also show you how to insert sample data.

Connecting to your Atlas cluster

After the previous section, you should have a new MongoDB cluster deployed in Atlas, a new database user and password, and some sample datasets loaded into your cluster.

Now, it’s time to connect to a database using your driver. In order to establish a database connection, your driver needs instructions on how to connect. These instructions are called the connection string. You can get your connection string for the cluster and username you created in the previous step by going into your Atlas account, navigating to the Database section, and clicking “Connect”.

A pop-up will appear prompting you to select your driver and version. Once you’ve populated both of those drop-downs, you can use the resulting database connection string (it’ll begin with mongdb+srv:// ) to connect to your cluster. Note that you will need to swap in the values for your own username and password!

You can use this connection string to run queries on your MongoDB cluster from your application. You can find a more in-depth set of instructions to fetch the connection string here. Example code to connect to the MongoDB Atlas cluster and execute a ping command is shared below:

 

C++

 

Documents and Collections

Unlike SQL databases, MongoDB stores information in documents and collections.

Documents

MongoDB stores data records as BSON documents. BSON is a binary representation of JSON documents, though it contains more data types than JSON. Documents are composed of field-value pairs, with the following structure:

 

Python

 

The values associated with fields can be any BSON data type, including other documents, arrays, and even arrays of documents. Field names themselves are strings.

Documents are much more flexible than standard rows in relational databases, as they give you a dynamic schema rather than an enforced one (though you can perform schema validation with MongoDB if you want).

Collections

MongoDB stores documents in collections. A collection is like a table in a relational database. For more details on how to create a collection, and other information about collections, you can check out the documentation here.

CRUD Operations

CRUD (create, read, update, and delete) operations are the four basic operations you might want to do with a database. In the case of MongoDB, you can use CRUD operations to insert documents, to write queries for matching documents, to update documents, and to delete documents. You can do this by using MQL, the MongoDB Query Language. Unlike SQL statements, MQL uses the same syntax as documents, making it intuitive and easy to use for even advanced querying.

A full C++ database tutorial as well as example code for each of the four CRUD operations using the C++ driver can be found in the MongoDB Developer Center and documentation.

Getting Help

The best part of using MongoDB is the vibrant community that includes users with all levels of experience with the C++ driver. The best way to get support for general questions is to use MongoDB Community Forums.

If you're running into an unexpected error, you think you've found a bug in the C++ driver, or you have a feature request, please open a JIRA ticket in the CXX project.

Tickets created in JIRA for both the C++ driver and the Core Server are public by default.

You can also raise feature requests on MongoDB Feedback and garner support by getting upvotes on the idea.

Resources

FAQs

Get Started With MongoDB Atlas

Try Free