[Question] Filter on array of objects

I want to query objects with a filter on a property that is an array of objects. Let’s say the property is tags:

[
{uniqueid: 123, name: ‘tag1’},
{uniqueid: 456, name: ‘tag2’,
]

I want to get objects that contain a tag with unique id 456 in its tags property. Basically, I want to filter on an array of objects by checking if an object exists in the array and then get those objects that match this criteria.

I checked the documentation, and this didn’t seem currently possible (I might have missed something though). What alternate methods could I try here?

For example, I tried using ContaintsAny:

       tenant_collection =  nodes_collection.with_tenant(name)
		
		query_result = tenant_collection.query.fetch_objects(
			filters=Filter.by_property("tags").contains_any([{'name': 'work', 'color': '#D5BA04', 'uniqueid': '14b06e9a-10e1-4255-a2b2-d414ecb27821'}],
			include_vector=True
		)

But it throws an error, and I believe this functionality might not exist yet.
The error thrown anyways is:

UUID input should be a string, bytes or UUID object [type=uuid_type, input_value={'name': 'work', 'color':...4', 'uniqueid': 'sdfsd'}, input_type=dict]

Hey @Tejas_Sharma,

Happy Monday! I hope you’re having a great start to the week!

I understand you’re trying to filter by inside arrays of objects, and I imagine your data looks something like this:

{
  "tags": [
    { "uniqueid": 123, "name": "tag1" },
    { "uniqueid": 456, "name": "tag2" }
  ]
}

If you’re attempting to filter based on an entire property rather than the values within it, nested-filters can help:

However, if you’re trying to filter inside arrays of nested objects, this feature is currently not supported. I would say open a feature request here:

As a potential workaround, you might consider flattening your data structure and storing the uniqueids in a separate array for more straightforward filtering.

Let me know if this helps or if you have any other questions!

1 Like

Ah I see Mohamed, I’ll submit a request there and it would indeed be very helpful.

And thanks for the alternate workaround, that’s very smart!

1 Like