For AI agents: a documentation index is available at https://www.mongodb.com/docs/llms.txt — markdown versions of all pages are available by appending .md to any URL path.
Docs Menu

Create Documents

In MongoDB, a Create operation consists of inserting documents into a collection. This page provides examples.

You can insert documents in MongoDB by using the following methods:

Note

Creating a Collection

Inserting a document into a collection that doesn't exist creates that collection.

If the collection does not currently exist, insert operations create the collection.

In MongoDB, each document stored in a standard collection requires a unique _id field that acts as a primary key. If an inserted document omits the _id field, the MongoDB driver automatically generates an ObjectId for the _id field.

This also applies to documents inserted through update operations with upsert: true.

Note

Atomicity and Write Acknowledgement

MongoDB write operations are atomic on the level of a single document. For more information, see Atomicity and Transactions.

You can also use write concerns to specify the level of acknowledgment requested from MongoDB for write operations. For more information, see Write Concern.

Earn a Skill Badge

Master "CRUD Operations" for free!

Learn more

On this page