Docs Menu
Docs Home
/ /

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:

  • Your programming language's driver.

  • The MongoDB Atlas UI. To learn more, see Insert Documents in the MongoDB Atlas UI.

  • MongoDB Compass.

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.

Back

CRUD Operations

Earn a Skill Badge

Master "CRUD Operations" for free!

Learn more

On this page