BlogAnnounced at MongoDB.local NYC 2024: A recap of all announcements and updates — Learn more >

MongoDB and C

C remains a strong choice for legacy and modern applications because of its performance, efficiency and general purpose usability. Most major operating systems like Windows, Mac, and Linux are powered by C. C is also used for embedded systems, gaming, and many more real-world use cases. The seamless integration of MongoDB with C language comes as no surprise, whether you’re working on a local setup, or an on-premise installation, or opting for the cloud-based MongoDB Atlas. With the MongoDB C Driver, integrating MongoDB into your development workflow becomes effortless.

Are you trying to find out which database to use with C application that you are developing? Follow along this guide to learn how to leverage C’s capabilities alongside MongoDB’s flexible and scalable database system.

Table of Contents

Why use MongoDB with C

MongoDB, a leading NoSQL database, offers distinct advantages for C developers. Its document-based model accommodates diverse data structures within a collection, allowing for seamless adjustments in data layout without extensive model modifications—a boon for C's flexibility.

  • 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 and can increase productivity by allowing changes in data layout without having to modify your entire data model.
  • Performance: MongoDB's native support for sharding and replication ensures high availability, horizontal scaling, and global distribution, catering well to C applications demanding robustness and speed.
  • Scalability: MongoDB's design facilitates effortless horizontal scaling by adding more servers, in contrast to the limitations of vertical scaling (more powerful hardware) common in SQL databases.
  • 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.

For developers venturing into new projects requiring a database, MongoDB Atlas stands out as a fully-managed, cloud-native solution. Offering diverse features like full-text search, vector search, charts, along with partner integrations, it provides a comprehensive environment for C-based applications.

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 libmongoc or mongo-c-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. We also have a 15 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

Following the setup of your MongoDB cluster in Atlas, along with a new user, password, and sample datasets, the next step involves establishing a connection to your database using the driver.

To connect, your driver requires a connection string (also known as URI) - a set of instructions for establishing the connection. Access your specific connection string for the created cluster and username by navigating to the Database section in your Atlas account and selecting "Connect ". Upon selection, a pop-up will prompt you to specify your driver and version. Once done, you'll receive a database connection string (starting with "mongdb+srv:// ") tailored to your cluster. Remember to substitute your username and password within this string!

You can use this connection string to connect to your MongoDB cluster from your application and perform different database operations and queries. 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:

 

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:

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

The fundamental CRUD (Create, Read, Update, Delete) operations are essential when interacting with a database. In MongoDB, these operations involve inserting, querying to match, updating, and deleting documents. This is accomplished through MQL (MongoDB Query Language), which, unlike SQL, employs a syntax akin to documents, ensuring an intuitive and straightforward querying experience, even for complex tasks.

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 CDRIVER 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 feedback.mongodb.com and garner support by getting upvotes on the idea.

Resources

FAQ

Get Started With MongoDB Atlas

Try Free