In a mobile app, I would like to enable users to create their own tags for each day of a set of events, each event having a variable number of days (from 1 to 15 days). Most events will be only 1 day.
For instance:
Event1 (2 days event)
- day1 {tag1:true, tag2: true}
- day2 {tag2: true, tag3: true}
Event2 (1 days event)
- day1 {tag3:true, tag4: true, tag5: true}
Then I would like to find all events for which one of the days has a specific set of tags. For instance, all events for which one of the days has the combination (tag1 and tag2) i.e. day1 of event1
Storing tags in maps with true as value should make it possible to compound where queries, one per tag, and check that their value is true. So I could search and find the (AND) combination of tags for each day.
But... what would be the simplest way to iterate the search for each day when the number of days might vary?
- I could program to read the number of days for each event, then check if the combination of tags exist for each of the days of that event... feels like a lot of work.
- It seems that a TAGS sub-collection and CollectionGroup would not work because it would require the number of days to be known in advance - unless I pre-set that all events to last the maximum number of days... does not feel optimum either
Any idea on how to best structure and query my events, days and tags?
CodePudding user response:
Yo could always add a searchable field to your event doc to satisfy your read pattern.
const event = {
...rest,
flatTags: ["openingDay-race", "finals-race", "afterParty-awards-interviews"],
};
This way you can simply add an index to this field and search the whole collection for the right tag combination (or hash).
I would keep it up to date by listening to the source of truth node for the tag metadata in the document. Then the doc CRUD logic doesn't have to change at all.