How to filter records based in a multiple aggregation (list) in Opa?

146 views Asked by At

I've several objects that can be tagged, so the model is

type Content = {
  ...
  tags : list(int)
  ...   
}

How can I avoid loading all the records from MongoDB to check if the content is tagged by all the tags from a given list (AND logic)? or at list one of the tags of the given list (OR logic)?

I've tried flatting the list in a string to use regex, but I'm also stuck there as you can see here

I'm using this version of opa (I'm stuck with it for the moment)

OPA version 0.9.2
(c) 2007-2012 MLstate, All Rights Reserved.
Build: 1815-(fe5cc09)
2

There are 2 answers

0
Quentin Bourgerie On

Probably using the hole expression [_], something like that

/db/path/to/content[tags[_] == "value"]

0
Marcin Skórzewski On

You can find documents with any tag from the list l with:

/db/coll[tags[_] in l]

For the reference look here.

For the "AND logic" its more complicated. MongoDB have $all operator, but it seems to be not implemented in Opa (at least it is not documented) :(

You can run Mongo JSON query

{ tags: { $all: [ 1, 2, 3 ] } }

with low level MongoDB support. Alternatively, you can ask multiple times for the tag:

/db/coll[tags[_] == frst and tags[_] == scnd and tags[_] == next];

But Opa does not support dynamic construction of DB queries, so you have to know the length of the list a priori (when writing the code).