gogson
gogson

Reputation: 950

Convert variant type to BSON

I have the following variant:

type variant = {one} / {two:int} / {three:string}

Using the opa2doc() method, I want to be able to ask my MongoDB database for all rows that contains a specific variant value (one, two or three) without taking into account the values for each variant. I'm using the low-level mongodb API.

I tried something like:

opa2doc({ kind={two=_} })

but it doesn't works.

It works for opa2doc({ kind={one} }) because this variant doesn't have any attached datas.

Any idea how to make this work ?

Upvotes: 0

Views: 95

Answers (1)

Marcin Skórzewski
Marcin Skórzewski

Reputation: 2854

If you want to use binding to native MongoDB query API you probably need to build a proper MongoDB query document. Queries finding a pattern (instead of comparing with equality) use special query operators which are JSON values. Opa's high-level DB API do this for you using its own, ML-like, operators. Native MongoDB query document for your case would need $exists operator:

{ "kind.two": { $exists: true } }

In old syntax it should be:

Bson.opa2doc({ `kind.two`= { `$exists`= true } })

Upvotes: 1

Related Questions