#community-help

Querying Nested Fields in a Collection with Dynamic Contributors

TLDR Greg asked about querying nested fields with unknown number of contributors in collection. Jason suggested dynamically fetching the collection's schema.

Powered by Struct AI

1

3
13mo
Solved
Join the chat
Sep 29, 2022 (13 months ago)
Greg
Photo of md5-11c0f771a29e2aa8d72ae9544cc39017
Greg
11:00 PM
Sorry to keep pestering you. From my example above, is there a way to query the nested fields if I don’t know exactly what those nested fields are up front?
For example, we may have up to 20 contributors in the list with keys contributor1, contributor2…contributor20. It may grow beyond 20. The test records I have now only go up to contributor5. I am dynamically adding contributors 1 through 20 in the query, but when I run the query I just get an error: Could not find a field named 'contributors.contributor6.firstName' in the schema. despite the fact that I have 1-5.
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
11:05 PM
As documents with those additional contributorX fields are added to the collection, that’s when they get added as a query-able field to the schema. You could fetch the collection’s schema dynamically (and may be cache it somewhere for a period of time) to get all the fields in the current schema and use that in the query_by field
Greg
Photo of md5-11c0f771a29e2aa8d72ae9544cc39017
Greg
11:06 PM
Ok. That’s what I was thinking. Thanks for the confirmation.

1