What would be the best way to filter records from a view based on tags in couchbase? I mean I understand that I can extract tags from the documents and use them as the keys using the map functionality in the view. But is there a way to select records from the view based on whether the record’s key (which can be multiple values) contains a tag?
Example: I have a transaction that has the following tags (tag 1, tag2, tag3, tag4) and each view record (from the emit function) looks like this
emit ([tag1,tag2,tag3,tag4], null)
What I want to get is all the records that have tag2.
Thanks for any help.
Two suggestions… If the only purpose of the view is to find records with a particular tag, you could simply emit a view row for each tag.
This map function would give you rows as follows:
When you query by key (e.g., key=”a”) you’ll get the results for that key. If however, your concern is “give me all documents where the second tag is ‘xyz'” then you could emit the index as well.
In this example, the rows would like like:
You would then query with an array key to get a key at a particular position (e.g., key=[1, “b”]).