---
title: "MongoDB® - Connect with Python"
description: "Learn how to connect to a MongoDB® database instance with one of the world's most famous programming languages: Python."
url: https://docs.us.ovhcloud.com/en/public-cloud/databases/mongodb-connect-python
lang: en
---
> For AI agents: the complete documentation index is available at https://docs.us.ovhcloud.com/en/llms.txt, the full documentation bundle is available at https://docs.us.ovhcloud.com/en/llms-full.txt.

# MongoDB® - Connect with Python

**Learn how to connect to a MongoDB® database instance with one of the world's most famous programming languages: Python.**

Cloud Databases allow you to focus on building and deploying cloud applications while OVHcloud takes care of the database infrastructure and maintenance in operational conditions.

You can find an example in the [GitHub examples repository](https://github.com/ovh/public-cloud-databases-examples/tree/main/databases/mongodb/python/hello-world).

 

## Requirements

- A [Public Cloud project](https://us.ovhcloud.com/public-cloud/) in your OVHcloud account
- Access to the [OVHcloud Control Panel](https://manager.us.ovhcloud.com/)
- A MongoDB[®](#go-further) database running on your OVHcloud Public Cloud Databases ([this guide](https://docs.us.ovhcloud.com/en/public-cloud/databases/getting-started.md) can help you to meet this requirement)
- [Configure your MongoDB® instance](https://docs.us.ovhcloud.com/en/public-cloud/databases/mongodb-configure-instance-accept-incoming-connections.md) to accept incoming connections
- A Python environment with a stable version and public network connectivity (Internet). This guide was made using Python 3.9.5.

 

## Concept

A MongoDB® instance can be managed in multiple ways. One of the easiest, yet powerful, is to use a Command Line Interface (CLI), as shown in our guide: [Connect to MongoDB® with CLI](https://docs.us.ovhcloud.com/en/public-cloud/databases/mongodb-connect-cli.md).

Another way is to interact directly using a programming language, such as Python. Python is one of the major programming languages in the world, especially in the Data ecosystem. MongoDB® provides official Python drivers, allowing us to connect and manage a MongoDB® instance from code.

To do so, we will need to set up our Python environment with MongoDB® drivers, then configure our Cloud Databases for MongoDB® instances to accept incoming connections, and finally code in Python to perform a few example actions.

 

## Instructions

### Set up your Python environment

**Note:**

Some useful tools, such as Conda, allow you to create and use specific Python environments on the same machine, like your computer. Conda will allow you to build a specific environment when working with MongoDB®, without impacting your existing configurations. For more information please visit [the Conda website](https://www.conda.io/).

To interact with your MongoDB® instance using Python, your development environment needs to be configured with:

- A compatible version of Python.
- MongoDB® official driver, called PyMongo, or Motor, when working asynchronously with MongoDB®.

This guide will focus on the use of **PyMongo**, which covers most of the use cases.

Please follow the official [MongoDB® documentation for Python drivers](https://docs.mongodb.com/drivers/python/) to get the latest information.

At the end of the documentation page for PyMongo, you will find a **compatibility matrix** between Python versions and PyMongo drivers. Follow this accordingly.

Once your Python environment is set up and you begin executing a **python --version** in your command line interface (CLI), you should see information about the version as shown below:

```python
laptop$ python --version
Python 3.10.12
```

In the same console, by typing **pip list | grep pymongo** check if **pymongo** is correctly installed:

```python
laptop$ pip list | grep pymongo
pymongo    4.10.1
```

 

### Configure your MongoDB® instance to accept incoming connections

In order to be able to connect to your MongoDB® instance, you will need to configure your instance to accept incoming connections from the IP address of your Python environment. If you don't know how to retrieve your IP, please visit a website such as [myip.ovh](https://myip.ovh/) from the station hosting the Python environment.

Once you know the IP address, follow the steps from this guide: [Configure your MongoDB® instance to accept incoming connections](https://docs.us.ovhcloud.com/en/public-cloud/databases/mongodb-configure-instance-accept-incoming-connections.md).

We will now follow the official MongoDB® documentation to perform our first connection with Python.

 

### Connect with Python

In your Python environment, let's try a connection. To be sure that we are indeed connected, we will insert two objects. Create a file called **basic\_insert.py**. Insert the code below with your own credentials.

As shown in the code, we use the **PyMongo** official driver. Use the [official PyMongo documentation](https://docs.mongodb.com/drivers/pymongo/) for an exhaustive command list.

```python
from pymongo import MongoClient
from pymongo.errors import ConnectionFailure

# Establishing connection

try:
    # MongoClient('mongodb://username:password@hostnameOrReplicaset/?tls=True') replica by your own Service URI
    uri = "mongodb+srv://<username>:<password>@mongodb-XXXXXXXX-XXXXXXXXX.database.cloud.ovh.us/admin?replicaSet=replicaset&tls=true"
    connect = MongoClient(uri)
    print("MongoDB cluster is reachable")
    print(connect)
except ConnectionFailure as e:
    print("Could not connect to MongoDB")
    print(e)
```

After executing your Python code, the result shown in the CLI should be like this:

```python
laptop$ python basic_insert.py
MongoDB cluster is reachable
MongoClient(host=['node1-123456789.database.cloud.ovh.us:27017'], document_class=dict, tz_aware=False, connect=True, ssl=True)
```

 

If not, your CLI should give you more details about the issue, for example:

- _Authentication failed_: could be an error with your user login or password;
- _nodename nor servname provided, or not known_: could be a wrongly typed hostname;
- _Connection reset by peer_: usually due to connection without secure TLS mode. use _tls=true_ parameter.

Once connected, you can perform multiple operations, for example, a few inserts and a **find()** inside a collection:

```python

# Connecting or switching to the database

db = connect.myDb

# Creating or switching to demoCollection

collection = db.demoCollection

#first document
document1 = {
        "name":"John",
        "age":24,
        "location":"New York"
        }
#second document
document2 = {
        "name":"Marianne",
        "age":21,
        "location":"Paris"
        }

# Inserting both documents one by one

collection.insert_one(document1)
collection.insert_one(document2)

# Printing the data inserted

cursor = collection.find()
for record in cursor:
    print(record)
```

In this example, we insert two objects inside a collection called **demoCollection**, then we perform a **find()** operations in this collection, looking for all the objects.

The result here is for the **print(record)**:

```python
{'_id': ObjectId('6140b59fd68c16b38fe677ba'), 'name': 'John', 'age': 24, 'location': 'New York'}
{'_id': ObjectId('6140b5a0d68c16b38fe677bb'), 'name': 'Marianne', 'age': 21, 'location': 'Paris'}
```

This code correctly found the two previously inserted objects. It means that here we were able to connect to the MongoDB® instance, insert data, and look for this data. Congratulations! Everything is working properly.

 

## Go further

Browse all the [Databases guides](https://docs.us.ovhcloud.com/en/public-cloud/databases/landing-page-databases.md).

Browse the rest of the [OVHcloud US documentation](https://docs.us.ovhcloud.com/en/index.md) for guides on our other products and services.

Learn more about [Managed Databases](https://us.ovhcloud.com/public-cloud/databases/) on the OVHcloud US website.

If you need training or technical assistance to implement our solutions, contact your sales representative or click on [this link](https://us.ovhcloud.com/professional-services/) to get a quote and ask our Professional Services experts for a custom analysis of your project.

OVHcloud Managed Databases and Analytics:

- Grafana® is a registered trademark of Grafana Labs and is used with the permission of Grafana Labs. OVH SAS and its subsidiaries are not affiliated with or endorsed by Grafana Labs.
- Kafka® is a registered trademark of The Apache Software Foundation and has been licensed for use by OVHcloud, who has no affiliation with and is not endorsed by The Apache Software Foundation.
- MongoDB® is a registered trademark of MongoDB, Inc.
- MySQL® is a registered trademark of Oracle and/or its affiliates.
- PostgreSQL® is a registered trademark of the PostgreSQL Community Association of Canada.
