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

# Multi-tenancy

In TopK, multi-tenancy is achieved through prefixing the tenant IDs in documents **`_id`** value.

This design enables TopK to scale a single collection efficiently without performance degradation by leveraging smart sharding by document ID.

### Storing documents for a specific tenant

To store documents for a specific tenant, prepend the **tenant ID** to the document **`_id`** value:

<CodeGroup>
  ```python Python theme={null}
  client.collection("books").upsert([
      {"_id": "tenant-1234_1", "title": "The Great Gatsby", "author": "F. Scott Fitzgerald"},
      {"_id": "tenant-1234_2", "title": "To Kill a Mockingbird", "author": "Harper Lee"},
  ])
  ```

  ```typescript Javascript theme={null}
  await client.collection("books").upsert([
    { _id: "tenant-1234_1", title: "The Great Gatsby", author: "F. Scott Fitzgerald" },
    { _id: "tenant-1234_2", title: "To Kill a Mockingbird", author: "Harper Lee" },
  ]);
  ```
</CodeGroup>

### Querying documents for a specific tenant

To query documents for a specific tenant, use the **tenant ID** along with the `startsWith()` filter:

<CodeGroup>
  ```python Python theme={null}
  client.collection("books").query(
      select(
          "title",
      )
      .filter(field("_id").startsWith("tenant-1234"))
  )
  ```

  ```typescript Javascript theme={null}
  await client.collection("books").query(
    select({
      title: field("title"),
    })
    .filter(field("_id").startsWith("tenant-1234"))
  );
  ```
</CodeGroup>

<Info>
  The approach preserves the ability to query across all tenants when required.
</Info>
