Docs Menu

Docs HomeStart with Guides

Insert Data into MongoDB

In this guide, you will insert data into MongoDB.

Time required: 15 minutes

1
2

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.

3

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

Tip

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

4

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

5

Here is the complete code followed by sample output.

Note

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
CRUD
  • 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 →