Insert Documents
On this page
➤ Use the Select your language drop-down menu in the upper-right to set the language of the following examples or select MongoDB Compass.
This page provides examples of insert operations in MongoDB.
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.
Note
Creating a Collection
If the collection does not currently exist, insert operations will create the collection.
Insert Documents in the MongoDB Atlas UI
To insert a document in the MongoDB Atlas UI, complete the following steps. To learn more about working with documents in the MongoDB Atlas UI, see Create, View, Update, and Delete Documents.
Add the documents
Click Insert Document.
Click the {} icon, which opens the JSON view.
Paste the document array into the text entry field. For example, the following entry creates four documents, each of which contain three fields:
[ { "prodId": 100, "price": 20, "quantity": 125 }, { "prodId": 101, "price": 10, "quantity": 234 }, { "prodId": 102, "price": 15, "quantity": 432 }, { "prodId": 103, "price": 17, "quantity": 320 } ]
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.