Why this matters
Every entity in Midaz supports metadata — custom key-value pairs that extend the standard data model. Queries that filter a large collection by a metadata field can become slow without an index. A metadata index is a MongoDB index on a specific metadata key. It turns an expensive collection scan into a fast indexed lookup. This matters most in production, where transaction volumes are high and you filter or sort by metadata values.
How it works
When you create a metadata index, Midaz builds a MongoDB index on the
metadata.<key> field of the entity collection. After that, any query that filters by the metadata key uses the index. MongoDB finds the documents directly, without a full collection scan.
Indexes are:
- Per-entity: Each index targets a specific entity type (e.g.,
transaction,operation). - Per-key: Each index covers a single metadata key.
- Optional uniqueness: You can require that no two documents share the same value for the indexed metadata key.
- Sparse by default: The index includes only documents that have the metadata key. This saves storage and speeds up writes.
Supported entity types
Midaz supports metadata indexes for these entities:
Creating a metadata index
Use the Create a Metadata Index endpoint:
Response:
Listing metadata indexes
Use the List Metadata Indexes endpoint. It returns all indexes across all entity types with their usage statistics:
stats.accesses field shows how many queries used the index since statistics collection started. Use it to find unused indexes that you can safely delete.
Deleting a metadata index
Use the Delete a Metadata Index endpoint:
Deletion takes effect immediately. It affects query performance for any operation that used the index. Before you delete an index, make sure no critical query depends on it.
Performance considerations
When to create indexes:
- You frequently filter transactions or operations by a specific metadata key (e.g.,
tier,channel,partner_id). - List queries on a large collection are slow when they filter by metadata.
- You need to enforce uniqueness on a metadata field (e.g., external reference IDs).
- You rarely query by the metadata key — the index only costs storage and slows writes.
- The collection is small enough that full scans are fast.
- You want to add an index speculatively, “just in case.”
0132 (Metadata Index Already Exists). Keep the index count deliberate: list the indexes and delete any with low or zero accesses.
Related pages
- Metadata — How metadata works across all Midaz entities.
- Create a Metadata Index — API reference.
- List Metadata Indexes — API reference.
- Delete a Metadata Index — API reference.

