---
title: "PyMongo"
description: "Learn about the PyMongo integration and how it adds support for connections to MongoDB databases."
url: https://docs.sentry.io/platforms/python/integrations/pymongo/
---

# PyMongo for Python

The PyMongo integration adds support for [PyMongo](https://www.mongodb.com/docs/drivers/pymongo/), the official MongoDB driver.

If you're using stream mode, this page's references to "transaction" should be applied to service spans instead. See [Streamed Spans](https://docs.sentry.io/platforms/python/tracing/streamed-spans.md) for more information.

## [Install](https://docs.sentry.io/platforms/python/integrations/pymongo.md#install)

Install `sentry-sdk` from PyPI:

```bash
pip install sentry-sdk
```

*Other available variations of the above snippet: uv*

## [Configure](https://docs.sentry.io/platforms/python/integrations/pymongo.md#configure)

If you have the `pymongo` package in your dependencies, the PyMongo integration will be enabled automatically when you initialize the Sentry SDK.

Error Monitoring\[ ]Tracing\[ ]Profiling

```python
import sentry_sdk

sentry_sdk.init(
    dsn="https://<key>@o<orgId>.ingest.sentry.io/<projectId>",
    # Check https://docs.sentry.io/platforms/python/data-management/data-collected/ to configure what data is collected
    data_collection={
        "user_info": True,
    },
    # ___PRODUCT_OPTION_START___ performance
    # Set traces_sample_rate to 1.0 to capture 100%
    # of transactions for tracing.
    traces_sample_rate=1.0,
    # ___PRODUCT_OPTION_END___ performance
    # ___PRODUCT_OPTION_START___ profiling
    # To collect profiles for all profile sessions,
    # set `profile_session_sample_rate` to 1.0.
    profile_session_sample_rate=1.0,
    # Profiles will be automatically collected while
    # there is an active span.
    profile_lifecycle="trace",
    # ___PRODUCT_OPTION_END___ profiling
)
```

## [Verify](https://docs.sentry.io/platforms/python/integrations/pymongo.md#verify)

```python
import pymongo

def main():
    sentry_sdk.init(...)  # same as above
    client = pymongo.MongoClient(DATABASE_HOST, DATABASE_PORT)

    db = client["sentry_test_db"]
    collection = db["sentry_test"]

    # or sentry_sdk.traces.start_span(name="your_span_name", parent_span=None) in stream mode
    with sentry_sdk.start_transaction(name="testing_sentry"):
        collection.insert_one({"test": "sentry"})
        collection.find_one({"test": "sentry"})

main()
```

This will create a transaction called `testing_sentry` in the Performance section of [sentry.io](https://sentry.io), and create spans for the database operations.

It takes a couple of moments for the data to appear in [sentry.io](https://sentry.io).

## [Behavior](https://docs.sentry.io/platforms/python/integrations/pymongo.md#behavior)

The following information about your MongoDB queries will be available to you on Sentry.io:

* Performance traces for all MongoDB queries
* Breadcrumbs for all MongoDB queries
* Personal identifiable information (PII) will be stripped from all MongoDB queries unless you enable `database_query_data` in the [`data_collection` option](https://docs.sentry.io/platforms/python/configuration/options.md#data_collection) or set `send_default_pii` to `True`. (This was tested for PyMongo 4.2 and below, but "should" also be future proof) `send_default_pii` will be removed in version 3.0 of the SDK.

### [Other MongoDB libraries](https://docs.sentry.io/platforms/python/integrations/pymongo.md#other-mongodb-libraries)

PyMongo is an official synchronous driver for MongoDB. It means that many other Python libraries interacting with MongoDB use it under the hood, like, for example `mongoengine` (always) or `umongo` (if selected from multiple available drivers). Official async MongoDB driver called `Motor` uses PyMongo under the hood as well. Queries generated by those libraries will also be monitored.

If you use a different driver (for example, `TxMongo`), this integration won't work.

### [Mongomock](https://docs.sentry.io/platforms/python/integrations/pymongo.md#mongomock)

While `mongomock` can be used to replace `PyMongo` in tests, it doesn't implement all features available in the official driver. This integration will not generate any breadcrumbs or spans from `mongomock`'s clients.

## [Supported Versions](https://docs.sentry.io/platforms/python/integrations/pymongo.md#supported-versions)

* PyMongo: 3.5.0+

* Python:

  * 3.6+ (SDK <3.0)
  * 3.7+ (SDK 3.0+)

If you're looking to use Sentry with older Python or framework versions, consider using an older major version of the SDK. The minimum versions for each major version are listed above.
