Insert Data into MongoDB¶
Author: MongoDB Documentation Team
In this guide, you will insert data into MongoDB.
Time required: 15 minutes
What You’ll Need¶
- If you have not already installed a client (e.g. a driver, MongoDB Compass, or the
mongo
shell), complete the Connect to MongoDB guide before attempting this guide.
- If you have not already installed a client (e.g. a driver, MongoDB Compass, or the
mongo
shell), complete the Connect to MongoDB guide before attempting this guide. - Enable Auth on your local instance of MongoDB.
Warning
If you are running MongoDB locally and have not enabled authentication, your MongoDB instance is not secure.
Check Your Environment¶
You will need to ensure that your MongoDB instance is running and accessible.
Check that you have an Atlas account and have deployed a MongoDB database cluster. See Get Started with Atlas for information on how to login and create a cluster in Atlas.
- Windows
- macOS
- Linux
To make sure that your MongoDB instance is running on Windows, run the following command from the Windows command prompt:
If a mongod.exe
instance is running, you will
see something like:
To make sure your MongoDB instance is running on Mac, run the following command from your terminal:
If a mongod
instance is running, you will see
something like:
To make sure your MongoDB instance is running on Linux, run the following command from your terminal:
If a mongod
instance is running, you will see
something like:
Procedure¶
Connect to Your MongoDB Instance¶
- Mongo Shell
- Compass
- Python
- Java (Sync)
- Node.js
- Other
Select the operating system platform on which you are running the MongoDB client you have selected.
- Windows
- macOS
- Linux
Pass the URI to the mongo shell followed by the --password
option. You will then be prompted for your password.
Pass the URI to the mongo shell followed by the --password
option. You will then be prompted for your password.
Pass the URI to the mongo shell followed by the --password
option. You will then be prompted for your password.
If you wish to manually configure your Compass connection, load
Compass and select the New Connection
link. You will see a
form where you can enter connection information for MongoDB.
Atlas users can copy a URI string from the Atlas console into Compass. MongoDB Compass can detect whether you have a MongoDB URI connection string in your system clipboard and auto- populate the connection dialog from the URI.
See Set Up Atlas Connectivity for information on how to get the Atlas connection string URI into your copy buffer.
If Compass was already running when you copied the URI string, click the NEW CONNECTION button.

You will be prompted to populate the connection dialog. Click Yes.
You should then populate the password field with the proper password for your MongoDB user in the connection form.
Note
Errors related to connecting through Compass will appear in red at the top of the Connect screen.
It’s a good idea to put your connection code in a class so that it can be reused.
If your connection_string
starts with mongodb+srv, you need to install the dnspython module with
Now add code to call the class you just created.
For the MongoDB java driver 3.7 and beyond, use the MongoClients.create()
method.
For legacy drivers (prior to 3.7), use:
The MongoDB.Bson
package is used in CRUD operations, so
you’ll import it here.
Replace your password and any parameters surrounded by $[]
in the connection string in the code below.
For now, you will use the context.TODO().
Later you’ll configure the context specific to your requirements.
You won’t know if the connection has been successful until you
use the connection. A ping is one way you can test the
connection. This is a full example of a Go connection to
mongoDB, including a test ping
.
In your Go workspace and project folder, run build.
Now run the binary. For binaries that are not installed, you’ll have to specify the path.
If you’d like to run the resulting binary without specifying a path, install the binary you just built into your Go workspace.
Now run the code. “yourprojectname” is the name of the project
directory that contains the file with your main()
function.
For installed binaries use:
For binaries that are not installed, you’ll have to specify the path.
The default timeout for the Go driver to connect to the database is 30 seconds. In the event that you are unable to connect, you will see an error that resembles this:
Switch to the test
Database¶
- Mongo Shell
- Compass
- Python
- Java (Sync)
- Node.js
- Other
To switch to the test
database in the mongo
shell, type
If the database has not been created already, click the Create Database button.

Select the test
database on the left side of the Compass
interface. Compass will list all of the collections in the
database below the database name.

To access the test
database:
Switch to the test
database and
access the inventory
collection.
Within the connect block, set db
to the test
database.
To access the test
database:
Switch to the test
database and
access the inventory
collection.
To access the test
database:
Insert a Single Document¶
Now you are ready to create your first document in MongoDB.
MongoDB stores documents as BSON, a binary form of JavaScript Object
Notation JSON
. The documents are stored in collections.
- Mongo Shell
- Compass
- Python
- Java (Sync)
- Node.js
- Other
The following example inserts a new document into the
inventory
collection. If the document does not specify
an _id
field, MongoDB adds the _id
field with an
ObjectId value to the new document.
To insert a single document using MongoDB Compass:
Navigate to the collection you wish to insert the document into:
- In the left-hand MongoDB Compass navigation pane, click the database to which your target collection belongs.
- From the database view, click the target collection name.
Click the Insert Document button:
For each field in the document, select the field type and fill in the field name and value. Add fields by clicking the last line number, then clicking Add Field After …
- For
Object
types, add nested fields by clicking the last field’s number and selecting Add Field After … - For
Array
types, add additional elements to the array by clicking the last element’s line number and selecting Add Array Element After …
- For
Once all fields have been filled out, click Insert.
The following example inserts a new document into the
test.inventory
collection:
The following example inserts a new document into the
inventory
collection. If the document does not specify
an _id
field, the PyMongo driver adds the _id
field
with an ObjectId value to the new document. See
Insert Behavior.
The following example inserts a new document into the
inventory
collection. If the document does not specify
an _id
field, the driver adds the _id
field with an
ObjectId value to the new document. See
Insert Behavior.
First, get the inventory collection.
The following example inserts a new document into the
inventory
collection. If the document does not specify
an _id
field, the Node.js driver adds the _id
field
with an ObjectId value to the new document. See
Insert Behavior.
The following example shows the api call required to insert a
new document into the inventory
collection. If the document
does not specify an _id
field, the Motor driver adds the
_id
field with an ObjectId value to the new document. See
Insert Behavior.
The following example inserts a new document into the
inventory
collection. If the document does not specify
an _id
field, the C# driver adds the _id
field
with an ObjectId value to the new document. See
Insert Behavior.
The following example shows the api call required to insert a
new document into the inventory
collection. If the document
does not specify an _id
field, the Go driver adds the
_id
field with an ObjectId value to the new document. See
Insert Behavior.
Before inserting the data, you’ll need to assign the
inventory
collection in the test
database to a
variable
- Mongo Shell
- Compass
- Python
- Java (Sync)
- Node.js
- Other

Next, populate a Document with fields and an embedded document, and insert it into the database.
For completeness, this is how you might wrap this call and run it with the asyncio event loop.
Run the loop:
Unlike SQL tables, MongoDB collections have dynamic schemas. That is, a single collection can store documents that differ in shapes (i.e. contain different fields and value types). And unlike SQL, no DDL operation is required to add or remove fields or modify field types. You just update the documents directly.
Note
While MongoDB does support dynamic collection definitions, you can also enforce a uniform schema for the documents in your collection. See JSON Schema validation.
- Python
- Java (Sync)
- Motor
When you are done working with your MongoDB data, close your connection to MongoDB:
Summary¶
If you have successfully completed this guide, you have created your first MongoDB data. In the next guide, you will check your work by retrieving the information you just saved.
What’s Next¶
In this guide, you will read all documents from a collection in the MongoDB database.