> For the complete documentation index, see [llms.txt](https://developer.harness.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://developer.harness.io/database-devops/use-db-devops/deployment-pipeline-configuration/use-jexl-expressions.md).

# Use expressions with database instance tags

Look up database instances by tag, or read the value of a tag on an instance, and use the result in your pipeline.

When you create a database instance in Harness, you can add tags to it. Harness Database DevOps provides two expressions that read those tags while a pipeline runs:

* `dbInstancesByTag` returns all the instances in a schema that have a given tag.
* `dbInstanceTagValue` returns the value of a tag on one instance.

Use them to run a step on every instance in an environment, or to run a stage only when an instance carries a certain tag. Both work in JEXL and CEL.

## Add tags to your instance

Add tags when you create or edit a database instance in **DB Instances**. A tag is either a key on its own, such as `allowAutomatedDeployment`, or a key-value pair, such as `env:prod`.

<figure><img src="https://4089095456-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FWwqb27iUU3gZvwGNroZp%2Fuploads%2Fgit-blob-d4b75ca515156cb4264d4ba32b7f53c58252282e%2Fdb-instance-tags.png?alt=media" alt="Tags field on a database instance"><figcaption></figcaption></figure>

Tags belong to the instance, not to the schema.

## Use an expression in a step

1. Open the step or stage setting you want the expression to drive.
2. Change the input type from **Fixed value** to **Expression**.
3. Enter the expression, for example `<+dbInstancesByTag.getValue("mySchema", "env:prod")>`.

<figure><img src="https://4089095456-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FWwqb27iUU3gZvwGNroZp%2Fuploads%2Fgit-blob-8492f0d4d895e16321014df351f187aed5d116f7%2Fdb-instance-expression-input.png?alt=media" alt="Changing a step input to Expression"><figcaption></figcaption></figure>

## Expression reference

### dbInstancesByTag

Returns the identifiers of all database instances in a schema that have the tag.

```
<+dbInstancesByTag.getValue("schema", "tagQuery")>
```

| Parameter  | Description                                                                          |
| ---------- | ------------------------------------------------------------------------------------ |
| `schema`   | The database schema to search.                                                       |
| `tagQuery` | The tag to match. Use a key, such as `env`, or a key-value pair, such as `env:prod`. |

If no instance matches, you get an empty list.

**Example**

```
<+dbInstancesByTag.getValue("mySchema", "env:prod")>
```

### dbInstanceTagValue

Returns the value of a tag on a specific instance.

```
<+dbInstanceTagValue.getValue("schema", "instance", "tagKey")>
```

| Parameter  | Description                                  |
| ---------- | -------------------------------------------- |
| `schema`   | The database schema the instance belongs to. |
| `instance` | The database instance to check.              |
| `tagKey`   | The key of the tag you want the value of.    |

If the tag isn't on the instance, you get an empty string.

**Example**

```
<+dbInstanceTagValue.getValue("mySchema", "instance1", "env")>
```

If `instance1` has the tag `env:prod`, this returns `prod`.

### CEL

Add the `cel.` prefix to call either expression from CEL:

```
${{ cel.dbInstancesByTag.getValue('mySchema', 'env:prod') }}
${{ cel.dbInstanceTagValue.getValue('mySchema', 'instance1', 'env') }}
```

## Examples

### Run a step on every instance with a tag

If your schema has four instances tagged `env:prod`, use `dbInstancesByTag` as the items in a repeat strategy. The stage runs once per instance.

```yaml
strategy:
  repeat:
    items: <+dbInstancesByTag.getValue("mySchema", "env:prod")>
```

Reference the current instance with `<+repeat.item>`.

Go to [Looping strategies](https://developer.harness.io/harness-platform/use-harness-platform/pipelines/looping-strategies/looping-strategies-matrix-repeat-and-parallelism) for more about repeat strategies.

### Filter the list by a second tag

Combine both expressions. Loop over the `env:prod` instances, then check each one's `region` tag to decide whether to run the step:

```
<+dbInstanceTagValue.getValue("mySchema", <+repeat.item>, "region")> == "us-east"
```

### Run a stage only when a tag matches

1. Select the stage, then go to **Advanced** > **Conditional Execution**.
2. Select **Execute this stage only if the following JEXL condition is true**.
3. Enter a condition, for example:

   ```
   <+dbInstanceTagValue.getValue("mySchema", "instance1", "env")> == "prod"
   ```

Harness skips the stage when the condition is false, instead of failing it.

Go to [Conditional executions for stages and steps](https://developer.harness.io/harness-platform/use-harness-platform/pipelines/step-skip-condition-settings) for more about conditions.

## Next steps

* [Create a pipeline in Database DevOps](https://developer.harness.io/database-devops/use-db-devops/deployment-pipeline-configuration/create-a-pipeline.md)
* [Apply a DB schema step](https://developer.harness.io/database-devops/use-db-devops/deployment-pipeline-configuration/step-types/apply-dbschema-step.md)
