Docs Menu

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.

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 acknowledgement requested from MongoDB for write operations. For details, see Write Concern.

Select your language

Give Feedback
MongoDB logo
© 2021 MongoDB, Inc.

About

  • Careers
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2021 MongoDB, Inc.