Docs Menu

Docs HomeMongoDB Manual

Insert Documents

On this page

  • Insert Documents in the MongoDB Atlas UI
  • Insert a Single Document
  • Insert Multiple Documents
  • Insert Behavior

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.

  • MongoDB Compass.

Note

Creating a Collection

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

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.

1
  1. In the MongoDB Atlas UI, click Database in the sidebar.

  2. For the database deployment to which you want to add documents, click Browse Collections.

  3. In the left navigation pane, select the database.

  4. In the left navigation pane, select the collection.

2
  1. Click Insert Document.

  2. Click the {} icon, which opens the JSON view.

  3. 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 }
    ]
3

MongoDB Atlas adds the documents to the collection.

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


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


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

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.

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

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

←  MongoDB CRUD OperationsInsert Methods →