> ## 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

export const NodeJSIcon = props => <svg xmlns="http://www.w3.org/2000/svg" className="h-6 w-6" width={64} height={64} viewBox="0 0 32 32" fill="#8cc84b" preserveAspectRatio="xMidYMid" {...props}>
    <path d="M14.656.427c.8-.453 1.82-.455 2.6 0L29.2 7.16c.747.42 1.247 1.253 1.24 2.114v13.5c.005.897-.544 1.748-1.332 2.16l-11.88 6.702a2.6 2.6 0 0 1-2.639-.073l-3.565-2.06c-.243-.145-.516-.26-.688-.495.152-.204.422-.23.642-.32.496-.158.95-.4 1.406-.656.115-.08.256-.05.366.022l3.04 1.758c.217.125.437-.04.623-.145l11.665-6.583a.386.386 0 0 0 .212-.38V9.334a.417.417 0 0 0-.25-.417L16.19 2.244a.41.41 0 0 0-.465-.001L3.892 8.93a.413.413 0 0 0-.25.415v13.37a.375.375 0 0 0 .215.375l3.162 1.785c.594.32 1.323.5 1.977.265a1.5 1.5 0 0 0 .971-1.409l.003-13.29c-.014-.197.172-.36.363-.34h1.52c.2-.005.357.207.33.405L12.18 23.88c.001 1.188-.487 2.48-1.586 3.063-1.354.7-3.028.553-4.366-.12l-3.4-1.88a2.448 2.448 0 0 1-1.332-2.16v-13.5a2.46 2.46 0 0 1 1.282-2.141L14.656.427zM18.1 9.785c1.727-.1 3.576-.066 5.13.785 1.203.652 1.87 2.02 1.892 3.358-.034.18-.222.28-.394.267-.5-.001-1.002.007-1.504-.003-.213.008-.336-.188-.363-.376-.144-.64-.493-1.273-1.095-1.582-.924-.463-1.996-.44-3.004-.43-.736.04-1.527.103-2.15.535-.48.328-.624 1-.453 1.522.16.383.603.506.964.62 2.082.544 4.287.5 6.33 1.207.845.292 1.672.86 1.962 1.745.378 1.186.213 2.604-.63 3.556-.684.784-1.68 1.2-2.675 1.442-1.323.295-2.695.302-4.038.17-1.263-.144-2.577-.476-3.552-1.336-.834-.724-1.24-1.852-1.2-2.94.01-.184.193-.312.37-.297h1.5c.202-.014.35.16.36.35.093.6.322 1.25.854 1.6 1.026.662 2.313.616 3.487.635.973-.043 2.065-.056 2.86-.7.42-.367.543-.98.43-1.508-.123-.446-.6-.653-1-.8-2.055-.65-4.285-.414-6.32-1.15-.826-.292-1.625-.844-1.942-1.693-.443-1.2-.24-2.687.693-3.607.9-.915 2.22-1.268 3.47-1.394z" />
  </svg>;

<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).
</Tip>

[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
  ([reach out to us](mailto:contact@codspeed.io) to enable)
* MongoDB instance running in your CI environment or the cloud

<Accordion title="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/)
</Accordion>

### 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@v4
  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@v4
    run: <your-benchmark-command>
    with:
      mode: simulation
      instruments: mongodb
      mongo-uri-env-name: MONGO_URL
    env:
      MONGO_URL: mongodb://localhost:27017
  ```
</Note>

<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@v4
    with:
      mode: simulation
      run: <your-benchmark-command>
      instruments: mongodb
  ```
</Note>

## 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.
</Tip>

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

<Columns cols={2}>
  <Card horizontal title="Node.js" href="/instruments/databases/mongodb/nodejs" icon={<NodeJSIcon />} />
</Columns>

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