Filtering based on contents of map field


#1

I have a type like:

entity type MyType {
  mapField: map(string, string)
}

i would like to fetch instances of MyType based on the contents of that map, something like:

MyType.fetch({filter: Filter.contains("{my_key: my_val}")})

But i can’t figure out the syntax… is there such a capability?


#2

Yes, something like this should work:

MyType.fetch({filter: Filter.eq("mapField['my_key']", "my_val")})

#3

Apparently this is not supported…
“Query with mapp lookup in expression “mapField[‘my_key’] == ‘my_val’))” is not yet supported; error at 1:0”


#4

From the db engine filter perspective, the following should work:

MyType.fetch({filter: “exists(mapField.key() == ‘my_key’ && mapField == ‘my_val’)”})

I’m not sure if the Filter thing supports building that but that is what works.


#5

Thanks tom, this worked great. To me it translates as:

Find instances of MyType where there exists a mapField who’s key is ‘my_key’ and who’s value is ‘my_val’.


#6

correct! - just adding more text to get over 20 character minimum response :slight_smile:


#7

Ah okay makes sense. I’ve seen mapField['my_key'] used in a stored calc expression, but I guess it doesn’t work in fetch filters.