Write Data to MongoDB
On this page
Overview
On this page, you can see copyable code examples that show common functions you can use to write data to MongoDB with the C driver.
Tip
To learn more about any of the functions shown on this page, see the link provided in each section.
To use an example from this page, copy the code example into the sample
application or your own application. Be sure to replace all
placeholders in the code examples, such as <connection string URI>
, with the
relevant values for your MongoDB deployment.
Sample Application
You can use the following sample application to test the code examples on this page. To use the sample application, perform the following steps:
Ensure you have the C driver installed.
Copy the following code and paste it into a new
.c
file.Copy a code example from this page and paste it on the specified lines in the file.
1 2 3 4 5 int 6 main (void) 7 { 8 mongoc_client_t *client; 9 mongoc_collection_t *collection; 10 char *str; 11 bson_error_t error; 12 13 mongoc_init (); 14 15 client = 16 mongoc_client_new ("<connection string URI>"); 17 collection = 18 mongoc_client_get_collection (client, "<database name>", "collection name"); 19 20 // Start example code here 21 22 // End example code here 23 24 mongoc_collection_destroy (collection); 25 mongoc_client_destroy (client); 26 mongoc_cleanup (); 27 28 return EXIT_SUCCESS; 29 }
Insert One
The following code shows how to insert a single document into a collection:
bson_t *document = BCON_NEW ("<field name>", BCON_UTF8 ("<value>")); bson_error_t error; if (!mongoc_collection_insert_one ( collection, document, NULL, NULL, &error)) { fprintf (stderr, "Insert one operation failed: %s\n", error.message); } bson_destroy (document);
To learn more about the mongoc_collection_insert_one()
function, see the
Insert Documents guide.
Insert Multiple
The following code shows how to insert multiple documents into a collection:
size_t num_docs = 2; bson_t *docs[num_docs]; docs[0] = BCON_NEW ("<field name>", BCON_UTF8 ("<value>")); docs[1] = BCON_NEW ("<field name>", BCON_UTF8 ("<value>")); bson_error_t error; if (!mongoc_collection_insert_many (collection, (const bson_t **) docs, num_docs, NULL, NULL, &error)) { fprintf (stderr, "Insert many operation failed: %s\n", error.message); } bson_destroy (docs[0]); bson_destroy (docs[1]);
To learn more about the mongoc_collection_insert_many()
function, see the
Insert Documents guide.
Update One
The following code shows how to update a single document in a collection by creating or editing a field:
bson_t *query = BCON_NEW ("<field to match>", BCON_UTF8 ("<value to match>")); bson_t *update = BCON_NEW ("$set", "{", "<field name>", BCON_UTF8 ("<value>"), "}"); bson_error_t error; if (!mongoc_collection_update_one (collection, query, update, NULL, NULL, &error)) { fprintf (stderr, "Update one operation failed: %s\n", error.message); } bson_destroy (query); bson_destroy (update);
To learn more about the mongoc_collection_update_one()
function, see the
Update Documents guide.
Update Multiple
The following code shows how to update multiple documents in a collection by creating or editing a field:
bson_t *query = BCON_NEW ("<field to match>", BCON_UTF8 ("<value to match>")); bson_t *update = BCON_NEW ("$set", "{", "<field name>", BCON_UTF8 ("<value>"), "}"); bson_error_t error; if (!mongoc_collection_update_many (collection, query, update, NULL, NULL, &error)) { fprintf (stderr, "Update many operation failed: %s\n", error.message); } bson_destroy (query); bson_destroy (update);
To learn more about the mongoc_collection_update_many()
function, see the
Update Documents guide.
Delete One
The following code shows how to delete a single document in a collection:
bson_t *filter = BCON_NEW ("<field name>", BCON_UTF8 ("<value>")); bson_error_t error; if (!mongoc_collection_delete_one (collection, filter, NULL, NULL, &error)) { fprintf (stderr, "Delete error: %s\n", error.message); } bson_destroy (filter);
To learn more about the mongoc_collection_delete_one()
function, see the
Delete Documents guide.
Delete Multiple
The following code shows how to delete multiple documents in a collection:
bson_t *filter = BCON_NEW ("<field name>", BCON_UTF8 ("<value>")); bson_error_t error; if (!mongoc_collection_delete_many (collection, filter, NULL, NULL, &error)) { fprintf (stderr, "Delete error: %s\n", error.message); } bson_destroy (filter);
To learn more about the mongoc_collection_delete_many()
function, see the
Delete Documents guide.
Bulk Write
The following code shows how to perform multiple write operations in a single bulk operation:
bson_error_t error; mongoc_bulk_operation_t *bulk = mongoc_collection_create_bulk_operation_with_opts (collection, NULL); bson_t *insert_doc = BCON_NEW ( "<field name>", BCON_UTF8 ("<value>"), "<field name>", BCON_UTF8 ("<value>"), "<field name>", BCON_UTF8 ("<value>"), "<field name>", BCON_UTF8 ("<value>") ); mongoc_bulk_operation_insert(bulk, insert_doc); bson_destroy (insert_doc); bson_t *query = BCON_NEW ("<field to match>", BCON_UTF8 ("<value to match>")); bson_t *update = BCON_NEW ("$set", "{", "<field name>", BCON_UTF8 ("<value>"), "}"); mongoc_bulk_operation_update_one(bulk, query, update, false); bson_destroy(query); bson_destroy(update); bool result = mongoc_bulk_operation_execute(bulk, NULL, &error); if (!result) { fprintf (stderr, "Bulk operation error: %s\n", error.message); } mongoc_bulk_operation_destroy (bulk);
To learn more about the mongoc_collection_bulk_operation_execute()
function, see the Bulk Write Operations guide.