Getting Started With MongoDB and C
Rate this tutorial
In this article we'll install the MongoDB C driver on macOS, and use this driver to write some sample console applications that can interact with your MongoDB data by performing basic CRUD operations. We'll use Visual Studio Code to type in the code and the command line to compile and run our programs. If you want to try it out now, all source code is in the GitHub repository.
- The sample dataset loaded into the Atlas cluster (or you can modify the sample code to use your own database and collection).
- Your machine’s IP address 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.
- We will use Visual Studio Code, available in macOS, Windows, and Linux, because it has official support for C code. Just download and install the appropriate version.
- We need the C extensions, which will be suggested when you open a C file for the first time. You can also open extensions and search for "C/C++" and install them. This will install several extensions: C/C++, C/C++ Themes, and CMake.
- The last step is to make sure we have a C compiler. For that, either install Xcode from the Mac App Store or run in a terminal:
1 $ xcode-select --install
Although we can use CMake to build our C applications (and you have detailed instructions on how to do it), we'll use VS Code to type our code in and the terminal to build and run our programs.
In macOS, if we have the package manager homebrew installed (which you should), then we just open a terminal and type in:
1 $ brew install mongo-c-driver
You can also download the source code and build the driver, but using brew is just way more convenient.
To make autocomplete work in VS Code, we need to change the extension's config to make sure it "sees" these new libraries installed. We want to change our INCLUDE_PATH to allow both IntelliSense to check our code while typing it and be able to build our app from VS Code.
To do that, from VS Code, open the
.vscode
hidden folder, and then click on c_cpp_properties.json and add these lines:1 { 2 "configurations": [ 3 { 4 "name": "Mac", 5 "includePath": [ 6 "/usr/local/include/libbson-1.0/**", 7 "/usr/local/include/libmongoc-1.0/**", 8 "${workspaceFolder}/**" 9 ], 10 ... 11 } 12 ] 13 }
Now, open tasks.json and add these lines to the args array:
1 "-I/usr/local/include/libmongoc-1.0", 2 "-I/usr/local/include/libbson-1.0", 3 "-lmongoc-1.0", 4 "-lbson-1.0",`
With these, we're telling VS Code where to find the MongoDB C libraries so it can compile and check our code as we type.
Let’s start with a simple program that connects to the MongoDB Atlas cluster and pings the server. For that, we need to get the connection string (URI) to the cluster and add it in our code. The best way is to create a new environment variable with the key “MONGODB_URI” and value the connection string (URI). It’s a good practice to keep the connection string decoupled from the code, but in this example, for simplicity, we'll have our connection string hardcoded.
We include the MongoDB driver and send an "echo" command from our
main
function. This example shows us how to initialize the MongoDB C client, how to create a command, manipulate JSON (in this case, BCON, BSON C Object Notation), send a command, process the response and error, and release any memory used.1 // hello_mongo.c 2 3 4 int main(int argc, char const *argv[]) { 5 // your MongoDB URI connection string 6 const char *uri_string = "mongodb+srv://<connection string>"; 7 // MongoDB URI created from above string 8 mongoc_uri_t *uri; 9 // MongoDB Client, used to connect to the DB 10 mongoc_client_t *client; 11 // Command to be sent, and reply 12 bson_t *command, reply; 13 // Error management 14 bson_error_t error; 15 // Misc 16 char *str; 17 bool retval; 18 19 /* 20 * Required to initialize libmongoc's internals 21 */ 22 mongoc_init(); 23 24 /* 25 * Optionally get MongoDB URI from command line 26 */ 27 if (argc > 1) { 28 uri_string = argv[1]; 29 } 30 31 /* 32 * Safely create a MongoDB URI object from the given string 33 */ 34 uri = mongoc_uri_new_with_error(uri_string, &error); 35 if (!uri) { 36 fprintf(stderr, 37 "failed to parse URI: %s\n" 38 "error message: %s\n", 39 uri_string, error.message); 40 return EXIT_FAILURE; 41 } 42 43 /* 44 * Create a new client instance, here we use the uri we just built 45 */ 46 client = mongoc_client_new_from_uri(uri); 47 if (!client) { 48 return EXIT_FAILURE; 49 } 50 51 /* 52 * Register the application name so we can track it in the profile logs 53 * on the server. This can also be done from the URI (see other examples). 54 */ 55 mongoc_client_set_appname(client, "connect-example"); 56 57 /* 58 * Do work. This example pings the database and prints the result as JSON 59 * BCON == BSON C Object Notation 60 */ 61 command = BCON_NEW("ping", BCON_INT32(1)); 62 63 // we run above command on our DB, using the client. We get reply and error 64 // (if any) 65 retval = mongoc_client_command_simple(client, "admin", command, NULL, &reply, 66 &error); 67 68 // mongoc_client_command_simple returns false and sets error if there are 69 // invalid arguments or a server or network error. 70 if (!retval) { 71 fprintf(stderr, "%s\n", error.message); 72 return EXIT_FAILURE; 73 } 74 75 // if we're here, there's a JSON response 76 str = bson_as_json(&reply, NULL); 77 printf("%s\n", str); 78 79 /* 80 * Clean up memory 81 */ 82 bson_destroy(&reply); 83 bson_destroy(command); 84 bson_free(str); 85 86 /* 87 * Release our handles and clean up libmongoc 88 */ 89 90 mongoc_uri_destroy(uri); 91 mongoc_client_destroy(client); 92 mongoc_cleanup(); 93 94 return EXIT_SUCCESS; 95 }
Although we can use way more sophisticated methods to compile and run our code, as this is just a C source code file and we're using just a few dependencies, I'll just compile from command line using good ol' gcc:
1 gcc -o hello_mongoc hello_mongoc.c \ 2 -I/usr/local/include/libbson-1.0 3 -I/usr/local/include/libmongoc-1.0 \ 4 -lmongoc-1.0 -lbson-1.0
To run the code, just call the built binary:
1 ./hello_mongo
In the repo that accompanies this post, you'll find a shell script that builds and runs all examples in one go.
Now that we have the skeleton of a C app, we can start using our database. In this case, we'll connect to the database
sample_mflix
, and we'll list all collections there.After connecting to the database, we list all connections with a simple
for
loop after getting all collection names with mongoc_database_get_collection_names
.1 if ((collection_names = 2 mongoc_database_get_collection_names(database, &error))) { 3 for (i = 0; collection_names[i]; i++) { 4 printf("%s\n", collection_names[i]); 5 } 6 7 }
The complete sample follows.
1 // list_collections.c 2 3 4 int main(int argc, char const *argv[]) { 5 // your MongoDB URI connection string 6 const char *uri_string = "mongodb+srv://<connection string>"; 7 8 // MongoDB URI created from above string 9 mongoc_uri_t *uri; 10 // MongoDB Client, used to connect to the DB 11 mongoc_client_t *client; 12 13 // Error management 14 bson_error_t error; 15 16 mongoc_database_t *database; 17 mongoc_collection_t *collection; 18 char **collection_names; 19 unsigned i; 20 21 /* 22 * Required to initialize libmongoc's internals 23 */ 24 mongoc_init(); 25 26 /* 27 * Safely create a MongoDB URI object from the given string 28 */ 29 uri = mongoc_uri_new_with_error(uri_string, &error); 30 if (!uri) { 31 fprintf(stderr, 32 "failed to parse URI: %s\n" 33 "error message: %s\n", 34 uri_string, error.message); 35 return EXIT_FAILURE; 36 } 37 38 /* 39 * Create a new client instance, here we use the uri we just built 40 */ 41 client = mongoc_client_new_from_uri(uri); 42 if (!client) { 43 return EXIT_FAILURE; 44 } 45 46 /* 47 * Register the application name so we can track it in the profile logs 48 * on the server. This can also be done from the URI (see other examples). 49 */ 50 mongoc_client_set_appname(client, "connect-example"); 51 52 /* 53 * Get a handle on the database "db_name" and collection "coll_name" 54 */ 55 database = mongoc_client_get_database(client, "sample_mflix"); 56 57 // getting all collection names, here we're not passing in any options 58 if ((collection_names = mongoc_database_get_collection_names_with_opts( 59 database, NULL, &error))) { 60 61 for (i = 0; collection_names[i]; i++) { 62 printf("%s\n", collection_names[i]); 63 } 64 65 } else { 66 fprintf(stderr, "Error: %s\n", error.message); 67 return EXIT_FAILURE; 68 } 69 70 /* 71 * Release our handles and clean up libmongoc 72 */ 73 74 mongoc_uri_destroy(uri); 75 mongoc_client_destroy(client); 76 mongoc_cleanup(); 77 78 return EXIT_SUCCESS; 79 }
If we compile and run it, we'l get this output:
1 $ ./list_collections 2 sessions 3 users 4 theaters 5 movies 6 comments
Being a document-based database, creating JSON documents is crucial for any application that interacts with MongoDB. Being this is C code, we don't use JSON. Instead, we use BCON (BSON C Object Notation, as mentioned above). To create a new document, we call
BCON_NEW
, and to convert it into a C string, we call bson_as_canonical_extended_json.
1 // bcon.c 2 // https://mongoc.org/libmongoc/current/tutorial.html#using-bcon 3 4 5 // Creating the JSON doc: 6 /* 7 { 8 born : ISODate("1906-12-09"), 9 died : ISODate("1992-01-01"), 10 name : { 11 first : "Grace", 12 last : "Hopper" 13 }, 14 languages : [ "MATH-MATIC", "FLOW-MATIC", "COBOL" ], 15 degrees: [ { degree: "BA", school: "Vassar" }, 16 { degree: "PhD", school: "Yale" } ] 17 } 18 */ 19 20 int main(int argc, char *argv[]) { 21 struct tm born = {0}; 22 struct tm died = {0}; 23 bson_t *document; 24 char *str; 25 26 born.tm_year = 6; 27 born.tm_mon = 11; 28 born.tm_mday = 9; 29 30 died.tm_year = 92; 31 died.tm_mon = 0; 32 died.tm_mday = 1; 33 34 // document = BCON_NEW("born", BCON_DATE_TIME(mktime(&born) * 1000), 35 // "died", BCON_DATE_TIME(mktime(&died) * 1000), 36 // "name", "{", 37 // "first", BCON_UTF8("Grace"), 38 // "last", BCON_UTF8("Hopper"), 39 // "}", 40 // "languages", "[", 41 // BCON_UTF8("MATH-MATIC"), 42 // BCON_UTF8("FLOW-MATIC"), 43 // BCON_UTF8("COBOL"), 44 // "]", 45 // "degrees", "[", 46 // "{", "degree", BCON_UTF8("BA"), "school", 47 // BCON_UTF8("Vassar"), "}", 48 // "{", "degree", BCON_UTF8("PhD"),"school", 49 // BCON_UTF8("Yale"), "}", 50 // "]"); 51 52 document = BCON_NEW("born", BCON_DATE_TIME(mktime(&born) * 1000), "died", 53 BCON_DATE_TIME(mktime(&died) * 1000), "name", "{", 54 "first", BCON_UTF8("Grace"), "last", BCON_UTF8("Hopper"), 55 "}", "languages", "[", BCON_UTF8("MATH-MATIC"), 56 BCON_UTF8("FLOW-MATIC"), BCON_UTF8("COBOL"), "]", 57 "degrees", "[", "{", "degree", BCON_UTF8("BA"), "school", 58 BCON_UTF8("Vassar"), "}", "{", "degree", BCON_UTF8("PhD"), 59 "school", BCON_UTF8("Yale"), "}", "]"); 60 61 /* 62 * Print the document as a JSON string. 63 */ 64 str = bson_as_canonical_extended_json(document, NULL); 65 printf("%s\n", str); 66 bson_free(str); 67 68 /* 69 * Clean up allocated bson documents. 70 */ 71 bson_destroy(document); 72 return 0; 73 }
Now that we've covered the basics of connecting to MongoDB, let's have a look at how to manipulate data.
Probably the most used function of any database is to retrieve data fast. In most use cases, we spend way more time accessing data than inserting or updating that same data. In this case, after creating our MongoDB client connection, we call
mongoc_collection_find_with_opts
, which will find data based on a query we can pass in. Once we have results, we can iterate through the returned cursor and do something with that data:1 // All movies from 1984! 2 BSON_APPEND_INT32(query, "year", 1984); 3 cursor = mongoc_collection_find_with_opts(collection, query, NULL, NULL); 4 5 while (mongoc_cursor_next(cursor, &query)) { 6 str = bson_as_canonical_extended_json(query, NULL); 7 printf("%s\n", str); 8 bson_free(str); 9 }
The complete sample follows.
1 // find.c 2 3 4 5 int main(int argc, char const *argv[]) { 6 // your MongoDB URI connection string 7 const char *uri_string = MY_MONGODB_URI; 8 // MongoDB URI created from above string 9 mongoc_uri_t *uri; 10 // MongoDB Client, used to connect to the DB 11 mongoc_client_t *client; 12 13 // Error management 14 bson_error_t error; 15 16 mongoc_collection_t *collection; 17 char **collection_names; 18 unsigned i; 19 20 // Query object 21 bson_t *query; 22 mongoc_cursor_t *cursor; 23 24 char *str; 25 26 /* 27 * Required to initialize libmongoc's internals 28 */ 29 mongoc_init(); 30 31 /* 32 * Safely create a MongoDB URI object from the given string 33 */ 34 uri = mongoc_uri_new_with_error(uri_string, &error); 35 if (!uri) { 36 fprintf(stderr, 37 "failed to parse URI: %s\n" 38 "error message: %s\n", 39 uri_string, error.message); 40 return EXIT_FAILURE; 41 } 42 43 /* 44 * Create a new client instance, here we use the uri we just built 45 */ 46 client = mongoc_client_new_from_uri(uri); 47 if (!client) { 48 puts("Error connecting!"); 49 return EXIT_FAILURE; 50 } 51 52 /* 53 * Register the application name so we can track it in the profile logs 54 * on the server. This can also be done from the URI (see other examples). 55 */ 56 mongoc_client_set_appname(client, "connect-example"); 57 58 /* 59 * Get a handle on the database "db_name" and collection "coll_name" 60 */ 61 collection = mongoc_client_get_collection(client, "sample_mflix", "movies"); 62 63 query = bson_new(); 64 65 // All movies from 1984! 66 BSON_APPEND_INT32(query, "year", 1984); 67 cursor = mongoc_collection_find_with_opts(collection, query, NULL, NULL); 68 69 while (mongoc_cursor_next(cursor, &query)) { 70 str = bson_as_canonical_extended_json(query, NULL); 71 printf("%s\n", str); 72 bson_free(str); 73 } 74 75 /* 76 * Release our handles and clean up libmongoc 77 */ 78 79 bson_destroy(query); 80 81 mongoc_collection_destroy(collection); 82 mongoc_uri_destroy(uri); 83 mongoc_client_destroy(client); 84 mongoc_cleanup(); 85 86 return EXIT_SUCCESS; 87 }
OK, we know how to read data, but how about inserting fresh data in our MongoDB database? It's easy! We just create a BSON document to be inserted and call
mongoc_collection_insert_one.
1 doc = bson_new(); 2 bson_oid_init(&oid, NULL); 3 BSON_APPEND_OID(doc, "_id", &oid); 4 BSON_APPEND_UTF8(doc, "name", "My super new picture"); 5 6 if (!mongoc_collection_insert_one(collection, doc, NULL, NULL, &error)) { 7 fprintf(stderr, "%s\n", error.message); 8 }
The complete sample follows.
1 // insert.c 2 3 4 5 int main(int argc, char const *argv[]) { 6 // your MongoDB URI connection string 7 const char *uri_string = MY_MONGODB_URI; 8 // MongoDB URI created from above string 9 mongoc_uri_t *uri; 10 // MongoDB Client, used to connect to the DB 11 mongoc_client_t *client; 12 13 // Error management 14 bson_error_t error; 15 16 mongoc_collection_t *collection; 17 char **collection_names; 18 unsigned i; 19 20 // Object id and BSON doc 21 bson_oid_t oid; 22 bson_t *doc; 23 24 char *str; 25 26 /* 27 * Required to initialize libmongoc's internals 28 */ 29 mongoc_init(); 30 31 /* 32 * Safely create a MongoDB URI object from the given string 33 */ 34 uri = mongoc_uri_new_with_error(uri_string, &error); 35 if (!uri) { 36 fprintf(stderr, 37 "failed to parse URI: %s\n" 38 "error message: %s\n", 39 uri_string, error.message); 40 return EXIT_FAILURE; 41 } 42 43 /* 44 * Create a new client instance, here we use the uri we just built 45 */ 46 client = mongoc_client_new_from_uri(uri); 47 if (!client) { 48 return EXIT_FAILURE; 49 } 50 51 /* 52 * Register the application name so we can track it in the profile logs 53 * on the server. This can also be done from the URI (see other examples). 54 */ 55 mongoc_client_set_appname(client, "connect-example"); 56 57 /* 58 * Get a handle on the database "db_name" and collection "coll_name" 59 */ 60 collection = mongoc_client_get_collection(client, "sample_mflix", "movies"); 61 62 doc = bson_new(); 63 bson_oid_init(&oid, NULL); 64 BSON_APPEND_OID(doc, "_id", &oid); 65 BSON_APPEND_UTF8(doc, "name", "My super new picture"); 66 67 if (!mongoc_collection_insert_one(collection, doc, NULL, NULL, &error)) { 68 fprintf(stderr, "%s\n", error.message); 69 } else { 70 printf("Document inserted!"); 71 /* 72 * Print the document as a JSON string. 73 */ 74 str = bson_as_canonical_extended_json(doc, NULL); 75 printf("%s\n", str); 76 bson_free(str); 77 } 78 79 /* 80 * Release our handles and clean up libmongoc 81 */ 82 83 mongoc_collection_destroy(collection); 84 mongoc_uri_destroy(uri); 85 mongoc_client_destroy(client); 86 mongoc_cleanup(); 87 88 return EXIT_SUCCESS; 89 }
To delete a document, we call
mongoc_collection_delete_one.
We need to pass in a document containing the query to restrict the documents we want to find and delete.1 doc = bson_new(); 2 BSON_APPEND_OID(doc, "_id", &oid); 3 4 if (!mongoc_collection_delete_one(collection, doc, NULL, NULL, &error)) { 5 fprintf(stderr, "Delete failed: %s\n", error.message); 6 }
The complete sample follows.
1 // delete.c 2 3 4 5 int main(int argc, char const *argv[]) { 6 // your MongoDB URI connection string 7 const char *uri_string = MY_MONGODB_URI; 8 // MongoDB URI created from above string 9 mongoc_uri_t *uri; 10 // MongoDB Client, used to connect to the DB 11 mongoc_client_t *client; 12 13 // Error management 14 bson_error_t error; 15 16 mongoc_collection_t *collection; 17 char **collection_names; 18 unsigned i; 19 20 // Object id and BSON doc 21 bson_oid_t oid; 22 bson_t *doc; 23 24 char *str; 25 26 /* 27 * Required to initialize libmongoc's internals 28 */ 29 mongoc_init(); 30 31 /* 32 * Safely create a MongoDB URI object from the given string 33 */ 34 uri = mongoc_uri_new_with_error(uri_string, &error); 35 if (!uri) { 36 fprintf(stderr, 37 "failed to parse URI: %s\n" 38 "error message: %s\n", 39 uri_string, error.message); 40 return EXIT_FAILURE; 41 } 42 43 /* 44 * Create a new client instance, here we use the uri we just built 45 */ 46 client = mongoc_client_new_from_uri(uri); 47 if (!client) { 48 return EXIT_FAILURE; 49 } 50 51 /* 52 * Register the application name so we can track it in the profile logs 53 * on the server. This can also be done from the URI (see other examples). 54 */ 55 mongoc_client_set_appname(client, "connect-example"); 56 57 /* 58 * Get a handle on the database "db_name" and collection "coll_name" 59 */ 60 collection = mongoc_client_get_collection(client, "sample_mflix", "movies"); 61 62 // Let's insert one document in this collection! 63 doc = bson_new(); 64 bson_oid_init(&oid, NULL); 65 BSON_APPEND_OID(doc, "_id", &oid); 66 BSON_APPEND_UTF8(doc, "name", "My super new picture"); 67 68 if (!mongoc_collection_insert_one(collection, doc, NULL, NULL, &error)) { 69 fprintf(stderr, "%s\n", error.message); 70 } else { 71 printf("Document inserted!"); 72 /* 73 * Print the document as a JSON string. 74 */ 75 str = bson_as_canonical_extended_json(doc, NULL); 76 printf("%s\n", str); 77 bson_free(str); 78 } 79 80 bson_destroy(doc); 81 82 // Delete the inserted document! 83 84 doc = bson_new(); 85 BSON_APPEND_OID(doc, "_id", &oid); 86 87 if (!mongoc_collection_delete_one(collection, doc, NULL, NULL, &error)) { 88 fprintf(stderr, "Delete failed: %s\n", error.message); 89 } else { 90 puts("Document deleted!"); 91 } 92 93 /* 94 * Release our handles and clean up libmongoc 95 */ 96 97 mongoc_collection_destroy(collection); 98 mongoc_uri_destroy(uri); 99 mongoc_client_destroy(client); 100 mongoc_cleanup(); 101 102 return EXIT_SUCCESS; 103 }
Finally, to update a document, we need to provide the query to find the document to update and a document with the fields we want to change.
1 query = BCON_NEW("_id", BCON_OID(&oid)); 2 update = 3 BCON_NEW("$set", "{", "name", BCON_UTF8("Super new movie was boring"), 4 "updated", BCON_BOOL(true), "}"); 5 6 if (!mongoc_collection_update_one(collection, query, update, NULL, NULL, 7 &error)) { 8 fprintf(stderr, "%s\n", error.message); 9 }
The complete sample follows.
1 // update.c 2 3 4 5 int main(int argc, char const *argv[]) { 6 // your MongoDB URI connection string 7 const char *uri_string = MY_MONGODB_URI; 8 // MongoDB URI created from above string 9 mongoc_uri_t *uri; 10 // MongoDB Client, used to connect to the DB 11 mongoc_client_t *client; 12 13 // Error management 14 bson_error_t error; 15 16 mongoc_collection_t *collection; 17 char **collection_names; 18 unsigned i; 19 20 // Object id and BSON doc 21 bson_oid_t oid; 22 bson_t *doc; 23 24 // document to update and query to find it 25 bson_t *update = NULL; 26 bson_t *query = NULL; 27 char *str; 28 29 /* 30 * Required to initialize libmongoc's internals 31 */ 32 mongoc_init(); 33 34 /* 35 * Safely create a MongoDB URI object from the given string 36 */ 37 uri = mongoc_uri_new_with_error(uri_string, &error); 38 if (!uri) { 39 fprintf(stderr, 40 "failed to parse URI: %s\n" 41 "error message: %s\n", 42 uri_string, error.message); 43 return EXIT_FAILURE; 44 } 45 46 /* 47 * Create a new client instance, here we use the uri we just built 48 */ 49 client = mongoc_client_new_from_uri(uri); 50 if (!client) { 51 return EXIT_FAILURE; 52 } 53 54 /* 55 * Register the application name so we can track it in the profile logs 56 * on the server. This can also be done from the URI (see other examples). 57 */ 58 mongoc_client_set_appname(client, "connect-example"); 59 60 /* 61 * Get a handle on the database "db_name" and collection "coll_name" 62 */ 63 collection = mongoc_client_get_collection(client, "sample_mflix", "movies"); 64 65 // we create a new BSON Document 66 doc = bson_new(); 67 bson_oid_init(&oid, NULL); 68 BSON_APPEND_OID(doc, "_id", &oid); 69 BSON_APPEND_UTF8(doc, "name", "My super new movie"); 70 71 // Then we insert it in the movies collection 72 if (!mongoc_collection_insert_one(collection, doc, NULL, NULL, &error)) { 73 fprintf(stderr, "%s\n", error.message); 74 } else { 75 printf("Document inserted!\n"); 76 /* 77 * Print the document as a JSON string. 78 */ 79 str = bson_as_canonical_extended_json(doc, NULL); 80 printf("%s\n", str); 81 bson_free(str); 82 83 // now we search for that document to update it 84 query = BCON_NEW("_id", BCON_OID(&oid)); 85 update = 86 BCON_NEW("$set", "{", "name", BCON_UTF8("Super new movie was boring"), 87 "updated", BCON_BOOL(true), "}"); 88 89 if (!mongoc_collection_update_one(collection, query, update, NULL, NULL, 90 &error)) { 91 fprintf(stderr, "%s\n", error.message); 92 } else { 93 printf("Document edited!\n"); 94 str = bson_as_canonical_extended_json(update, NULL); 95 printf("%s\n", str); 96 } 97 } 98 99 /* 100 * Release our handles and clean up libmongoc 101 */ 102 103 if (doc) { 104 bson_destroy(doc); 105 } 106 if (query) { 107 bson_destroy(query); 108 } 109 if (update) { 110 bson_destroy(update); 111 } 112 113 mongoc_collection_destroy(collection); 114 mongoc_uri_destroy(uri); 115 mongoc_client_destroy(client); 116 mongoc_cleanup(); 117 118 return EXIT_SUCCESS; 119 }
With this article, we covered the installation of the MongoDB C driver, configuring VS Code as our editor and setting up other tools. Then, we created a few console applications that connect to MongoDB Atlas and perform basic CRUD operations.
Get more information about the C driver, and to try this code, the easiest way would be to register for a free MongoDB account. We can't wait to see what you build next!