Overview
In this guide, you can learn how to use the C++ driver to add documents to a MongoDB collection by performing insert operations.
An insert operation inserts one or more documents into a MongoDB collection.
You can perform an insert operation by using the insert_one() method to
insert a single document or the insert_many() method to insert one or more
documents.
Sample Data
The examples in this guide use the sample_restaurants.restaurants collection
from the Atlas sample datasets. To access this collection
from your C++ application, instantiate a client that connects to an Atlas cluster
and assign the following values to your db and collection variables:
auto db = client["sample_restaurants"]; auto collection = db["restaurants"];
To learn how to create a free MongoDB Atlas cluster and load the sample datasets, see the Get Started with Atlas guide.
The _id Field
In a MongoDB collection, each document must contain an _id field
with a unique field value.
MongoDB allows you to manage this field in two ways:
You can set this field for each document yourself, ensuring each
_idfield value is unique.You can let the driver automatically generate unique
ObjectIdvalues for each document_id.
Unless you can guarantee uniqueness, we recommend
letting the driver automatically generate _id values.
Note
Duplicate _id values violate unique index constraints, which
causes the driver to return a mongocxx::bulk_write_exception
error.
To learn more about the _id field, see the
Unique Indexes guide in the MongoDB Server manual.
To learn more about document structure and rules, see the Documents guide in the MongoDB Server manual.
Insert One Document
To add a single document to a MongoDB collection, call the insert_one()
method and pass the document you want to add.
The following example inserts a document into the restaurants collection:
auto result = collection.insert_one(make_document(kvp("name", "Mongo's Burgers")));
Insert Multiple Documents
To add multiple documents to a MongoDB collection, call the insert_many()
method and pass a vector that stores the documents you want to add.
The following example inserts two documents into the restaurants collection:
std::vector<bsoncxx::document::value> restaurants; restaurants.push_back(make_document(kvp("name", "Mongo's Burgers"))); restaurants.push_back(make_document(kvp("name", "Mongo's Pizza"))); auto result = collection.insert_many(restaurants);
Modify Insert Behavior
You can modify the behavior of the insert_one() and insert_many() methods by
passing an instance of the mongocxx::options::insert class as an optional
parameter. The following table describes the fields you can set in a
mongocxx::options::insert instance:
Field | Description |
|---|---|
| If set to true, allows the write to opt out of
document-level validation.Defaults to false.Type: bool |
| Sets the write concern for the operation. Defaults to the write concern of the namespace. Type: mongocxx::write_concern |
| If set to true, the operation stops inserting documents when one insert
fails. If false, the operation continues to insert the remaining documents
when one insert fails.Defaults to true.Type: bool |
| A comment to attach to the operation. For more information, see the insert command
fields guide in the
MongoDB Server manual. Type: bsoncxx::types::bson_value::view_or_value |
Example
The following code uses the insert_many() method to insert three new
documents into a collection. Because the bypass_document_validation field
is set to true in a mongocxx::options::insert instance, this
insert operation bypasses document-level validation:
std::vector<bsoncxx::document::value> docs; docs.push_back(make_document(kvp("name", "Mongo's Burgers"))); docs.push_back(make_document(kvp("name", "Mongo's Pizza"))); docs.push_back(make_document(kvp("name", "Mongo's Tacos"))); mongocxx::options::insert opts; opts.bypass_document_validation(true); auto result = collection.insert_many(docs, opts);
Additional Information
For runnable code examples of inserting documents with the C++ driver, see Write Data to MongoDB.
API Documentation
To learn more about any of the methods or types discussed in this guide, see the following API documentation: