Docs Menu

Docs HomeStart with Guides

Insert Data into MongoDB

In this guide, you will insert data into MongoDB.

Time required: 15 minutes


Switch to the database and collection you want to work with. In this case you will be using the sample_guides database and comets collection.


Create and insert three new documents for the comets collection. Each document consists of the following information about the comet:

  • The name

  • The official name

  • The orbital period in years

  • The radius in miles

  • The mass in kilograms


If you omit the _id field, the driver automatically generates a unique ObjectId value for the _id field.


Many write operations in MongoDB return a result object that contains information about the operation.


Here is the complete code followed by sample output.


Your ObjectId values will differ from those shown.

If you have completed this guide, you have inserted data into MongoDB.

In the next guide, you will learn how to update a field in a document.

See the following resources for more in-depth information about the concepts presented here:

  • Insert Documents

  • Insert Methods

What's Next
Update Data in MongoDB
10 mins

Specify which documents to update and how to update them in MongoDB.

Start Guide
Chapter 2
  • Add a MongoDB Driver
  • Read Data in MongoDB
  • Read Data from MongoDB With Queries
  • Read Data using Operators and Compound Queries
  • Insert Data into MongoDB
  • Update Data in MongoDB
  • Delete Data from MongoDB
Start with Guides →