Search inside an intmap collection for a specific attribute (not using map index)

73 views Asked by At

My question is (again) about OPA database high-level API.

I have the following data-model declared:

database click = @mongo
db /click/click : intmap(Scan.scan)

and here's the Scan.scan type:

type Scan.scan = {
  user : int
  qr : int
  date : Date.date
}

My problem here is that I need to query the database for specifics "clicks" for a given "qr" attribute, but the fact that an intmap is used forces me to retrieve all the clicks, and then manually search in the entire list for a match with the given "qr" parameter.

What I'm doing actually is fetching all the clicks via /click/click and then applying a List.filter on it....but I have performance issues (and I have the feeling that I'm doing MongoDB's work)

So my question is quite simple: Did I miss something with the OPA database high-level API, or do I necessarily need to do a data migration on my click collection as I can't retrieve the intmap Index value that isn't stored anywhere else.

I hope my question is clear enough. Thanks for your replies.

1

There are 1 answers

1
o-town On BEST ANSWER

What I usually do in this situation is build another index, e.g.

db /click/by_qr : intmap(Scan.scan)

or

db /click/by_qr : intmap(int) // int represents user id

Of course, this means you have to be careful to maintain the index. My understanding is that there will eventually be a better way to handle this, but right now you have to do it manually; I agree it's not a very nice solution.