Docs Menu

Docs HomeMongoDB Drivers

MongoDB C++ Driver

On this page

  • Introduction
  • Installation
  • Connect to MongoDB Atlas
  • Connect to MongoDB Atlas Without the Stable API
  • Connect to a MongoDB Server on Your Local Machine
  • Compatibility

Welcome to the documentation site for the official MongoDB C++ driver. You can add the driver to your application to work with MongoDB using the C++11 or later standard. Download the library, mongocxx, from mongocxx.org or set up a runnable project by following our tutorial.

See Installing the mongocxx driver.

You can use the following connection snippet to test your connection to your MongoDB deployment on Atlas:

#include <bsoncxx/json.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
int main()
{
try
{
// Create an instance.
mongocxx::instance inst{};
// Replace the connection string with your MongoDB deployment's connection string.
const auto uri = mongocxx::uri{"<connection string>"};
// Set the version of the Stable API on the client.
mongocxx::options::client client_options;
const auto api = mongocxx::options::server_api{ mongocxx::options::server_api::version::k_version_1 };
client_options.server_api_opts(api);
// Setup the connection and get a handle on the "admin" database.
mongocxx::client conn{ uri, client_options };
mongocxx::database db = conn["admin"];
// Ping the database.
const auto ping_cmd = bsoncxx::builder::basic::make_document(bsoncxx::builder::basic::kvp("ping", 1));
db.run_command(ping_cmd.view());
std::cout << "Pinged your deployment. You successfully connected to MongoDB!" << std::endl;
}
catch (const std::exception& e)
{
// Handle errors.
std::cout<< "Exception: " << e.what() << std::endl;
}
return 0;
}

This connection snippet uses the Stable API feature which you can use when connecting to MongoDB Server v5.0 and later and the C++ driver v3.7 and later.

When you use this feature, you can update your driver or server without worrying about backward compatibility issues with any commands covered by the Stable API.

Note

Starting from February 2022, the Versioned API is known as the Stable API. All concepts and features remain the same with this naming change.

If you are using a version of MongoDB or the driver that lacks support for the Stable API, you can use the following code snippet to test your connection to your MongoDB deployment on Atlas:

#include <bsoncxx/json.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
int main()
{
try
{
// Create an instance.
mongocxx::instance inst{};
// Replace the connection string with your MongoDB deployment's connection string.
const auto uri = mongocxx::uri{"<connection string>"};
// Setup the connection and get a handle on the "admin" database.
mongocxx::client conn{ uri };
mongocxx::database db = conn["admin"];
// Ping the database.
const auto ping_cmd = bsoncxx::builder::basic::make_document(bsoncxx::builder::basic::kvp("ping", 1));
db.run_command(ping_cmd.view());
std::cout << "Pinged your deployment. You successfully connected to MongoDB!" << std::endl;
}
catch (const std::exception& e)
{
// Handle errors.
std::cout<< "Exception: " << e.what() << std::endl;
}
return 0;
}

If you need to run a MongoDB server on your local machine for development purposes instead of using an Atlas cluster, you need to complete the following:

  1. Download the Community or Enterprise version of MongoDB Server.

  2. Install and configure MongoDB Server.

  3. Start the server.

Important

Always secure your MongoDB server from malicious attacks. See our Security Checklist for a list of security recommendations.

After you successfully start your MongoDB server, specify your connection string in your driver connection code.

If your MongoDB Server is running locally, you can use the connection string "mongodb://localhost:<port>" where <port> is the port number you configured your server to listen for incoming connections.

If you need to specify a different hostname or IP address, see our Server Manual entry on Connection Strings.

To test whether you can connect to your server, replace the connection string in the Connect to MongoDB Atlas code example and run it.

The following compatibility table specifies the recommended version or versions of the MongoDB C++ driver for use with a specific version of MongoDB.

The first column lists the driver version.

Important

MongoDB ensures compatibility between the MongoDB Server and the drivers for three years after the server version's end of life (EOL) date. To learn more about the MongoDB release and EOL dates, see MongoDB Software Lifecycle Schedules.

Icon
Explanation
All features are supported.
The Driver version will work with the MongoDB version, but not all new MongoDB features are supported.
No mark
The Driver version is not tested with the MongoDB version.
C++ Driver Version
MongoDB 7.0
MongoDB 6.0
MongoDB 5.0
MongoDB 4.4
MongoDB 4.2
MongoDB 4.0
MongoDB 3.6
MongoDB 3.4
MongoDB 3.2
MongoDB 3.0
MongoDB 2.6
MongoDB 2.4
3.8+
3.7
3.6
3.5
3.4
3.3
3.2
3.1
3.0

The driver does not support older versions of MongoDB.

The following compatibility table specifies the recommended version or versions of the MongoDB C++ driver for use with a specific version of C++.

The first column lists the driver version.

C++ Driver Version
C++20
C++17
C++14
C++11
C++03
3.8+
<= 3.7
1.x

For more information on how to read the compatibility tables, see our guide on MongoDB Compatibility Tables.

←  MongoDB C DriverMongoDB .NET/C# Driver →