# Semantic conventions for MongoDB client operations

LLMS index: [llms.txt](/llms.txt)

---

**Status**: [Development][DocumentStatus]

<!-- START doctoc -->

- [Spans](#spans)
  - [Example](#example)
- [Metrics](#metrics)

<!-- END doctoc -->

The Semantic Conventions for [MongoDB](https://www.mongodb.com/) extend and override the [Database Semantic Conventions](README.md).

## Spans

<!-- semconv span.db.mongodb.client -->
<!-- NOTE: THIS TEXT IS AUTOGENERATED. DO NOT EDIT BY HAND. -->
<!-- see templates/registry/markdown/snippet.md.j2 -->
<!-- prettier-ignore-start -->

**Status:** ![Development](https://img.shields.io/badge/-development-blue)

Spans representing calls to MongoDB adhere to the general [Semantic Conventions for Database Client Spans](/docs/specs/semconv/db/database-spans.md).

`db.system.name` MUST be set to `"mongodb"` and SHOULD be provided **at span creation time**.
**Span name** SHOULD follow the general [database span name convention](/docs/specs/semconv/db/database-spans.md#name)

**Span kind** SHOULD be `CLIENT`.

**Span status** SHOULD follow the [Recording Errors](/docs/specs/semconv/general/recording-errors.md) document.

**Attributes:**

| Key | Stability | [Requirement Level](/docs/specs/semconv/general/attribute-requirement-level/) | Value Type | Description | Example Values |
| --- | --- | --- | --- | --- | --- |
| [`db.collection.name`](/docs/specs/semconv/registry/attributes/db.md) | ![Stable](https://img.shields.io/badge/-stable-lightgreen) | `Required` | string | The MongoDB collection being accessed within the database stated in `db.namespace`. [1] | `public.users`; `customers` |
| [`db.operation.name`](/docs/specs/semconv/registry/attributes/db.md) | ![Stable](https://img.shields.io/badge/-stable-lightgreen) | `Required` | string | The name of the [MongoDB command](https://www.mongodb.com/docs/manual/reference/command/) being executed. | `findAndModify`; `getMore`; `insertMany`; `bulkWrite` |
| [`db.namespace`](/docs/specs/semconv/registry/attributes/db.md) | ![Stable](https://img.shields.io/badge/-stable-lightgreen) | `Conditionally Required` If available. | string | The MongoDB database name. | `customers`; `test.users` |
| [`db.response.status_code`](/docs/specs/semconv/registry/attributes/db.md) | ![Stable](https://img.shields.io/badge/-stable-lightgreen) | `Conditionally Required` [2] | string | [MongoDB error code](https://www.mongodb.com/docs/manual/reference/error-codes/) represented as a string. [3] | `36`; `11602` |
| [`error.type`](/docs/specs/semconv/registry/attributes/error.md) | ![Stable](https://img.shields.io/badge/-stable-lightgreen) | `Conditionally Required` If and only if the operation failed. | string | Describes a class of error the operation ended with. [4] | `timeout`; `java.net.UnknownHostException`; `server_certificate_invalid`; `500` |
| [`server.port`](/docs/specs/semconv/registry/attributes/server.md) | ![Stable](https://img.shields.io/badge/-stable-lightgreen) | `Conditionally Required` [5] | int | Server port number. [6] | `80`; `8080`; `443` |
| [`db.operation.batch.size`](/docs/specs/semconv/registry/attributes/db.md) | ![Stable](https://img.shields.io/badge/-stable-lightgreen) | `Recommended` | int | The number of database operations included in a batch operation. [7] | `2`; `3`; `4` |
| [`server.address`](/docs/specs/semconv/registry/attributes/server.md) | ![Stable](https://img.shields.io/badge/-stable-lightgreen) | `Recommended` | string | Name of the database host. [8] | `example.com`; `10.1.2.80`; `/tmp/my.sock` |

**[1] `db.collection.name`:** It is RECOMMENDED to capture the value as provided by the application without attempting to do any case normalization.
For batch operations, if the individual operations are known to have the same collection name then that collection name SHOULD be used.

**[2] `db.response.status_code`:** If the operation failed and error code is available.

**[3] `db.response.status_code`:** All MongoDB error codes SHOULD be considered errors.

**[4] `error.type`:** The `error.type` SHOULD match the `db.response.status_code` returned by the database or the client library, or the canonical name of exception that occurred.
When using canonical exception type name, instrumentation SHOULD do the best effort to report the most relevant type. For example, if the original exception is wrapped into a generic one, the original exception SHOULD be preferred.
Instrumentations SHOULD document how `error.type` is populated.

**[5] `server.port`:** If using a port other than the default port for this DBMS and if `server.address` is set.

**[6] `server.port`:** When observed from the client side, and when communicating through an intermediary, `server.port` SHOULD represent the server port behind any intermediaries, for example proxies, if it's available.

**[7] `db.operation.batch.size`:** Except for empty batch requests described below, a batch operation contains two
or more database operations explicitly submitted as separate operations in a single
client call, protocol message, or database command.

Requests to batch APIs that contain only one operation SHOULD be modeled as single
operations, not as batch operations.

A database call is not a batch operation solely because one operation accepts
multiple operands, such as keys, rows, documents, points, or other data elements,
including Redis [`MGET`](https://redis.io/docs/latest/commands/mget/) with
multiple keys.

In batch APIs that execute the same parameterized operation with parameter sets,
each parameter set represents one database operation for determining whether the
request is a batch operation. Requests with only one parameter set SHOULD be modeled
as single operations, not as batch operations.

`db.operation.batch.size` SHOULD be set to the number of operations in the batch.
It SHOULD NOT be set for non-batch operations.

A request to execute a batch operation with no operations SHOULD also be treated
as a batch operation, and `db.operation.batch.size` SHOULD be set to `0`.

**[8] `server.address`:** When observed from the client side, and when communicating through an intermediary, `server.address` SHOULD represent the server address behind any intermediaries, for example proxies, if it's available.

The following attributes can be important for making sampling decisions
and SHOULD be provided **at span creation time** (if provided at all):

* [`db.collection.name`](/docs/specs/semconv/registry/attributes/db.md)
* [`db.namespace`](/docs/specs/semconv/registry/attributes/db.md)
* [`db.operation.name`](/docs/specs/semconv/registry/attributes/db.md)
* [`server.address`](/docs/specs/semconv/registry/attributes/server.md)
* [`server.port`](/docs/specs/semconv/registry/attributes/server.md)

---

`error.type` has the following list of well-known values. If one of them applies, then the respective value MUST be used; otherwise, a custom value MAY be used.

| Value | Description | Stability |
| --- | --- | --- |
| `_OTHER` | A fallback error value to be used when the instrumentation doesn't define a custom value. | ![Stable](https://img.shields.io/badge/-stable-lightgreen) |

<!-- prettier-ignore-end -->
<!-- END AUTOGENERATED TEXT -->
<!-- endsemconv -->

### Example

| Key                    | Value                      |
| :--------------------- | :------------------------- |
| Span name              | `"findAndModify products"` |
| `db.system.name`       | `"mongodb"`                |
| `server.address`       | `"mongodb0.example.com"`   |
| `server.port`          | `27017`                    |
| `db.collection.name`   | `"products"`               |
| `db.namespace`         | `"shopDb"`                 |
| `db.query.text`        | not set                    |
| `db.operation.name`    | `"findAndModify"`          |

## Metrics

MongoDB client instrumentations SHOULD collect metrics according to the general
[Semantic Conventions for Database Client Metrics](database-metrics.md).

`db.system.name` MUST be set to `"mongodb"`.

[DocumentStatus]: /docs/specs/otel/document-status
