# Advance Query Formatting

**URL:** <https://forums.losant.com/t/advance-query-formatting/5662>\
**Category:** Help\
**Tags:** workflow\
**Created:** [October 28, 2024, 2:16pm UTC](https://forums.losant.com/t/advance-query-formatting/5662 "2024-10-28T14:16:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tanner\_Williams](https://avatars.discourse-cdn.com/v4/letter/t/e274bd/32.png) [@Tanner\_Williams](https://forums.losant.com/u/Tanner_Williams)\
**Post date:** [October 28, 2024, 2:16pm UTC](https://forums.losant.com/t/advance-query-formatting/5662/1 "2024-10-28T14:16:47Z")

</div>

Help changing this query into something more like the one below it (except it works)

**Current**

```auto
for(const key in payload.data.query) {
    if(key.startsWith('tag_')) {
        and.push({
            "tags": {
                "$eq": {
                    "$tagKey": key.split('tag_')[1],
                    "$tagValue" : payload.data.query[key]
                },
            }
        });
    }
}

```

**Target**

```auto
for(const key in payload.data.query) {
    if(key.startsWith('tag_')) {
        and.push({
            "tags": {
                "$eq": {
                    "$tagKey": key.split('tag_')[1],
                    "$tagValue": {
                        "$contains" : payload.data.query[key]
                        }
                },
            }
        });
    }
}

```

---

<div class="post-metadata">

**Author:** ![Dylan\_Schuster](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/dylan_schuster/32/2850_2.png) [@Dylan\_Schuster](https://forums.losant.com/u/Dylan_Schuster)\
**Post date:** [October 28, 2024, 2:23pm UTC](https://forums.losant.com/t/advance-query-formatting/5662/2 "2024-10-28T14:23:52Z")

</div>

We don’t support `$contains` on `$tagValue` properties, so unfortunately your “target” isn’t going to work.

What’s the goal? Are your end users submitting a query with one or more tags and you want to return devices matching those tags? Can users submit more than one tag _key_ or is it only one key and multiple possible values? Should a device match _all_ tags in the query or just any of them? What else is in the `$and` array you are pushing these objects into?

---

<div class="post-metadata">

**Author:** ![Tanner\_Williams](https://avatars.discourse-cdn.com/v4/letter/t/e274bd/32.png) [@Tanner\_Williams](https://forums.losant.com/u/Tanner_Williams)\
**Post date:** [October 28, 2024, 2:33pm UTC](https://forums.losant.com/t/advance-query-formatting/5662/3 "2024-10-28T14:33:35Z")

</div>

![image](https://us1.discourse-cdn.com/flex015/uploads/getstructure/original/2X/f/f5a4aeb7bed4e3d4f830d6939fd3cf44437c2e19.png)

Main goal was just for the user to be able to not have to type in the full tag name out and just enter “met” or something similar, just kinda a quality of life thing , other than that its just the default device table query that came with cpf.

if youre not familiar with the default cpf i could send the code here

---

<div class="post-metadata">

**Author:** ![Dylan\_Schuster](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/dylan_schuster/32/2850_2.png) [@Dylan\_Schuster](https://forums.losant.com/u/Dylan_Schuster)\
**Post date:** [October 28, 2024, 2:42pm UTC](https://forums.losant.com/t/advance-query-formatting/5662/4 "2024-10-28T14:42:27Z")

</div>

Sorry, but we only [support `$eq` and `$ne` comparisons in tag queries](https://docs.losant.com/references/advanced-queries/#tag-comparisons). I’ll write a feature request to support partial string matches.

In the meantime for a QoL enhancement, you could adjust the table to instead put a `<select>` box in that text input and populate it only with valid values for the given tag. That would require looking up all the tag values for each tag key that has a column in the table (also filtered down to only the devices that user has access to) in the workflow that is rendering the table. The [Devices: Tag Values API endpoint](https://docs.losant.com/rest-api/devices/#tag-values) can provide this.

---

<div class="post-metadata">

**Author:** ![Tanner\_Williams](https://avatars.discourse-cdn.com/v4/letter/t/e274bd/32.png) [@Tanner\_Williams](https://forums.losant.com/u/Tanner_Williams)\
**Post date:** [October 28, 2024, 2:44pm UTC](https://forums.losant.com/t/advance-query-formatting/5662/5 "2024-10-28T14:44:12Z")

</div>

Alright thanks for the help and the ideas!
