Docs Menu
Docs Home
/ / /
C Driver
/

Insert Documents

On this page

  • Overview
  • Sample Data
  • The _id Field
  • Insert One Document
  • Insert Multiple Documents
  • Modify Insert Behavior
  • Example
  • Additional Information
  • API Documentation

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 following functions:

  • mongoc_collection_insert_one() function to insert a single document

  • mongoc_collection_insert_many() function to insert one or more documents

The examples in this guide use the restaurants collection in the sample_restaurants database from the Atlas sample datasets. To learn how to create a free MongoDB Atlas cluster and load the sample datasets, see the Get Started with Atlas guide.

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:

  • Set the _id field for each document yourself, ensuring each value is unique.

  • Let the driver automatically generate unique bson_oid_t values for each document _id field.

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 mongoc_bulkwriteexception_t 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.

To add a single document to a MongoDB collection, call the mongoc_collection_insert_one() function and pass the following parameters:

  • Collection into which you're inserting documents

  • Document to insert

  • Options to customize the operation, or NULL

  • Pointer to overwritable storage that will contain operation results, or NULL

  • Location for an error value, or NULL

The following example inserts a document into the restaurants collection:

bson_t *document = BCON_NEW ("name", BCON_UTF8 ("Mongo's Burgers"));
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 add multiple documents to a MongoDB collection, call the mongoc_collection_insert_many() function and pass the following parameters:

  • Collection into which you're inserting documents

  • Array of pointers to the documents you want to insert

  • Number of documents to insert

  • Options to customize the operation, or NULL

  • Pointer to overwritable storage that will contain operation results, or NULL

  • Location for an error value, or NULL

The following example inserts two documents into the restaurants collection:

size_t num_docs = 2;
bson_t *docs[num_docs];
docs[0] = BCON_NEW ("name", BCON_UTF8 ("Mongo's Burgers"));
docs[1] = BCON_NEW ("name", BCON_UTF8 ("Mongo's Pizza"));
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]);

You can modify the behavior of the mongoc_collection_insert_one() and mongoc_collection_insert_many() functions by passing a BSON document that specifies option values. The following table describes some options you can set in the document:

Option
Description

bypassDocumentValidation

If set to true, allows the write operation to opt out of document-level validation.
Defaults to false.
Type: bool

writeConcern

Sets the write concern for the operation.
Defaults to the write concern of the namespace.
Type: mongoc_write_concern_t

ordered

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. You cannot pass this option to the mongoc_collection_insert_one() function.
Defaults to true.
Type: bool

comment

A comment to attach to the operation. For more information, see the insert command fields guide in the MongoDB Server manual.
Type: bson_value_t

The following code uses the mongoc_collection_insert_many() function to insert three new documents into a collection. Because the bypassDocumentValidation field is set to true, this insert operation bypasses document-level validation:

size_t num_docs = 3;
bson_t *docs[num_docs];
docs[0] = BCON_NEW ("name", BCON_UTF8("Mongo's Burgers"));
docs[1] = BCON_NEW ("name", BCON_UTF8("Mongo's Pizza"));
docs[2] = BCON_NEW ("name", BCON_UTF8("Mongo's Tacos"));
bson_t opts;
bson_init (&opts);
bson_append_bool (&opts, "bypassDocumentValidation", -1, true);
bson_error_t error;
if (!mongoc_collection_insert_many (collection, (const bson_t **) docs, num_docs, &opts, NULL, &error)) {
fprintf (stderr, "Insert many operation failed: %s\n", error.message);
}
bson_destroy (docs[0]);
bson_destroy (docs[1]);
bson_destroy (docs[2]);
bson_destroy (&opts);

To learn more about any of the functions discussed in this guide, see the following API documentation:

Back

Write Data