Docs HomeMongoDB Manual

Insert Documents


➤ Use the Select your language drop-down menu in the upper-right to set the language of the examples on this page.


This page provides examples of insert operations in MongoDB.

Note

Creating a Collection

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

Insert a Single Document

To retrieve the document that you just inserted, query the collection:

Insert Multiple Documents


➤ Use the Select your language drop-down menu in the upper-right to set the language of the examples on this page.


Insert Behavior

Collection Creation

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

_id Field

In MongoDB, each document stored in a 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.

Atomicity

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

Write Acknowledgement

With write concerns, you can specify the level of acknowledgement requested from MongoDB for write operations. For details, see Write Concern.