Advance Query Formatting

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

Current

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

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]
                        }
                },
            }
        });
    }
}

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?

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

Sorry, but we only support $eq and $ne comparisons in tag queries. 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 can provide this.

1 Like

Alright thanks for the help and the ideas!