> ## Documentation Index
> Fetch the complete documentation index at: https://codspeed.io/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# MongoDB Instrumentation Overview

> MongoDB instrument to measure the performance of queries directly in the CI

> [!TIP]
>
> This feature is currently in beta and is subject to change. If you are
> interested in testing it out, please reach out to us via
> [Discord](https://discord.gg/MxpaCfKSqF) or
> [email out support](mailto:contact@codspeed.io).

[MongoDB](https://www.mongodb.com/) instrument to measure the performance of
queries directly in the CI.

## Getting Started

### Prerequisites

* MongoDB instrument enabled in your CodSpeed account, available as an add-on on
  the Team and Enterprise [plans](https://codspeed.io/docs/features/plans-and-billing.md)
  ([reach out to us](mailto:contact@codspeed.io) to enable)
* MongoDB instance running in your CI environment or the cloud

> [accordion] How to set up a MongoDB instance in your CI environment?
>
> We recommend: - using the
> [`art049/mongodb-cluster-action`](https://github.com/art049/mongodb-cluster-action)
> to run MongoDB in your CI environment - dynamic setup of a MongoDB instance
> during the setup phase of the benchmarks is supported, using tools such as
> [`testcontainers`](https://node.testcontainers.org/modules/mongodb/) or
> [`vitest-mongodb`](https://github.com/enochchau/vitest-mongodb/)

### Activate the `mongodb` instrument in the CodSpeed Action

To activate the MongoDB instrument during the run of your benchmarks in the
CodSpeed Action, make the following changes to your workflow file:

```yaml {6,11} theme={null}
- name: Run benchmarks
  uses: CodSpeedHQ/action@v5
  run: <your-benchmark-command>
  with:
    mode: simulation
    # Activate the MongoDB instrument
    instruments: mongodb
    # (Optional) Specify the name of the environment variable that contains the MongoDB
    # connection string and is used by your application to connect to MongoDB.
    # If not specified, you will have to provide the connection string dynamically during
    # the setup phase of your benchmarks.
    mongo-uri-env-name: MONGO_URL
```

> [!NOTE]
>
> **Using `mongo-uri-env-name`**
>
> If using the `mongo-uri-env-name` option, make sure to set the actual value of
> the environment variable in the `env` section of your workflow file. Otherwise,
> the MongoDB instrument will not be able to connect to your MongoDB instance.
>
> ```yaml {8} theme={null}
> - name: Run benchmarks
>   uses: CodSpeedHQ/action@v5
>   run: <your-benchmark-command>
>   with:
>     mode: simulation
>     instruments: mongodb
>     mongo-uri-env-name: MONGO_URL
>   env:
>     MONGO_URL: mongodb://localhost:27017
> ```

> [!NOTE]
>
> **Dynamic connection string**
>
> If the value of connection string cannot be set in the workflow file, you can
> omit the `mongo-uri-env-name` option and provide the connection string
> dynamically during the setup phase of your benchmarks.
>
> ```yaml theme={null}
> - name: Run benchmarks
>   uses: CodSpeedHQ/action@v5
>   with:
>     mode: simulation
>     run: <your-benchmark-command>
>     instruments: mongodb
> ```

## Language specific examples

> [!TIP]
>
> If you are using the `mongo-uri-env-name` option, the integration should work
> out of the box at this stage 🎉
>
> However, if you are not using the `mongo-uri-env-name` option, you will have to
> provide the connection string dynamically during the setup phase of your
> benchmarks. Select your language below to see how to do that.

To view examples and how to dynamically provide the connection string during the
setup phase of your benchmarks, select your language below:

> [card] [Node.js](https://codspeed.io/docs/instruments/databases/mongodb/nodejs.md)

* **Python**: Not supported yet (*coming soon*)
* **Rust**: Not supported yet (*coming soon*)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.