Write Data to MongoDB
On this page
Overview
On this page, you can see copyable code examples that show common C++ driver methods for writing data to MongoDB.
Tip
To learn more about any of the methods 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, such as <connection string>
, 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 in a location from which your project can import it.
Copy the following code and paste it into a new
.cpp
file within your project.Copy a code example from this page and paste it within the highlighted section of the file.
1 2 3 4 5 6 7 8 9 10 using bsoncxx::builder::basic::kvp; 11 using bsoncxx::builder::basic::make_document; 12 13 int main() { 14 try { 15 mongocxx::instance instance; 16 17 mongocxx::uri uri("<connection string>"); 18 mongocxx::client client(uri); 19 20 auto database = client["<database name>"]; 21 auto collection = database["<collection name>"]; 22 23 // Start example code here 24 25 // End example code here 26 27 } catch (const mongocxx::exception& e) { 28 std::cout << "An exception occurred: " << e.what() << "\n"; 29 return EXIT_FAILURE; 30 } 31 32 return EXIT_SUCCESS; 33 }
Insert One
The following code shows how to insert a single document into a collection:
auto result = collection.insert_one(make_document(kvp("<field name>", "<value>")));
To learn more about the insert_one()
method, see the Insert Documents guide.
Insert Multiple
The following code shows how to insert multiple documents into a collection:
std::vector<bsoncxx::document::value> documents; documents.push_back(make_document(kvp("<field name>", "<value>"))); documents.push_back(make_document(kvp("<field name>", "<value>"))); auto result = collection.insert_many(documents);
To learn more about the insert_many()
method, 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:
auto query_filter = make_document(kvp("<field to match>", "<value to match>")); auto update_doc = make_document(kvp("$set", make_document(kvp("<field name>", "<value>")))); auto result = collection.update_one(query_filter.view(), update_doc.view());
To learn more about the update_one()
method, 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:
auto query_filter = make_document(kvp("<field to match>", "<value to match>")); auto update_doc = make_document(kvp("$set", make_document(kvp("<field name>", "<value>")))); auto result = collection.update_many(query_filter.view(), update_doc.view());
To learn more about the update_many()
method, see the
Update Documents guide.
Replace One
The following code shows how to replace a single document in a collection:
auto query_filter = make_document(kvp("<field to match>", "<value to match>")); auto replace_doc = make_document(make_document(kvp("<field name>", "<value>"))); auto result = collection.replace_one(query_filter.view(), replace_doc.view());
To learn more about the replace_one()
method, see the
Replace Documents guide.
Delete One
The following code shows how to delete a single document in a collection:
auto result = collection.delete_one(make_document(kvp("<field name>", "<value>")));
To learn more about the delete_one()
method, see the
Delete Documents guide.
Delete Multiple
The following code shows how to delete multiple documents in a collection:
auto result = collection.delete_many(make_document(kvp("<field name>", "<value>")));
To learn more about the delete_many()
method, see the
Delete Documents guide.
Bulk Write
The following code shows how to perform multiple write operations in a single bulk operation:
auto bulk = collection.create_bulk_write(); auto insert_doc = make_document(kvp("<field name>", "<value>")); auto update_filter = make_document(kvp("<field name>", "<value>")); auto update_doc = make_document(kvp("$set", make_document(kvp("<field name>", "<value>")))); auto delete_filter = make_document(kvp("<field name>", "<value>")); mongocxx::model::insert_one insert_op{insert_doc.view()}; mongocxx::model::update_many update_op{update_filter.view(), update_doc.view()}; mongocxx::model::delete_one delete_op{delete_filter.view()}; bulk.append(insert_op); bulk.append(update_op); bulk.append(delete_op); auto result = bulk.execute();
To learn more about the create_bulk_write()
method, see the
Bulk Write guide.