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

# NodeJS setup for MongoDB instrumentation

> MongoDB instrument for NodeJS

<Tip>
  Make sure you have
  [activated the `mongodb` instrument](/instruments/databases/mongodb/#activate-the-mongodb-instrument-in-the-codspeed-action)
  in your CodSpeed GitHub workflow.
</Tip>

<Accordion title="Minimum integration versions">
  Make sure you are using the minimum required version of the NodeJS integrations:

  * [`@codspeed/vitest-plugin>=3.1.0`](https://github.com/CodSpeedHQ/codspeed-node/releases/tag/v3.1.0)
  * [`@codspeed/tinybench-plugin>=3.0.0`](https://github.com/CodSpeedHQ/codspeed-node/releases/tag/v3.0.0)
  * [`@codspeed/benchmark.js-plugin>=3.0.0`](https://github.com/CodSpeedHQ/codspeed-node/releases/tag/v3.0.0)
</Accordion>

## In-depth guides

To see complete guides of the different integrations, check out the pages below:

<Card title="Benchmarking a MongoDB based NestJS API with vitest" href="/instruments/databases/mongodb/nodejs/vitest">
  Setup and run MongoDB performance tests in a Node.js API using vitest,
  leveraging Docker.
</Card>

<Card title="Benchmarking a MongoDB based NestJS API with tinybench" href="/instruments/databases/mongodb/nodejs/tinybench">
  Setup and run MongoDB performance tests in a Node.js API using tinybench,
  leveraging Docker.
</Card>

## Dynamically providing the connection string

Each integration exports a `setupInstruments` function that can be used to
dynamically setup the instruments. This function takes the actual connection
string as an argument and returns the patched connection string that should be
used to connect to the database.

```typescript theme={null}
type SetupInstrumentsRequestBody = {
  /**
   * The full `MONGO_URL` that is usually used to connect to the database.
   */
  mongoUrl: string;
};

type SetupInstrumentsResponse = {
  /**
   * The patched `MONGO_URL` that should be used to connect to the database.
   */
  remoteAddr: string;
};

/**
 * Dynamically setup the CodSpeed instruments.
 */
declare function setupInstruments(
  body: SetupInstrumentsRequestBody
): Promise<SetupInstrumentsResponse>;
```

You can use this function to set up the instruments in your application:

```typescript src/bench.ts {12} theme={null}
import { setupInstruments, withCodSpeed } from "@codspeed/tinybench-plugin";
import { MongoDBContainer } from "@testcontainers/mongodb";
import { registerCatControllerBenches } from "cats/cats.controller.tinybench";
import { Bench } from "tinybench";

async function setupDatabase() {
  const mongodbContainer = await new MongoDBContainer("mongo:7.0.5").start();
  const mongoUrl =
    mongodbContainer.getConnectionString() +
    "/test?replicaSet=rs0&directConnection=true";

  const { remoteAddr } = await setupInstruments({ mongoUrl });
  process.env.MONGO_URL = remoteAddr;
}

const bench = withCodSpeed(new Bench());

(async () => {
  await setupDatabase();

  registerCatControllerBenches(bench);

  await bench.run();
  console.table(bench.table());
})();
```

The example above uses the
[`testcontainers`](https://node.testcontainers.org/modules/mongodb/) library to
start a MongoDB container and get the connection string. Then, the
`setupInstruments` function is used to patch the connection string and set it as
the `MONGO_URL` environment variable. Finally, the `bench` is run as usual.

<Warning>
  The `setupInstruments` function should be called **only once** during the whole
  benchmark run, and **before** any connection to the database is established.
  Otherwise, the CodSpeed MongoDB instrument will not be able to collect the
  metrics.
</Warning>
