hamiltop / rethinkdb-elixir

Rethinkdb client in pure elixir (JSON protocol)
MIT License
497 stars 64 forks source link

Negating contains #123

Closed pauliephonic closed 7 years ago

pauliephonic commented 7 years ago

Hi there, sorry to open an issue but not sure where to ask.

I want to select documents where an array property does not contain a value.

table("users") |> filter(lambda fn(user) -> contains(user["contacts"], lambda fn(contact) -> contact["id"] == contact_id end ) end) |> Database.run

The above code correctly return documents where the contacts property does contain an item whose id property is a certain value (contact_id).

If I naively negate the contains as !contains, I get zero results which is not correct:

table("users") |> filter(lambda fn(user) -> !contains(user["contacts"], lambda fn(contact) -> contact["id"] == contact_id end ) end) |> Database.run

In fact it seems that negating the contains always return zero rows regardless of the predicate. I'm obviously not understanding how the elixir code is getting transformed into the REQL expression.

How would I go about returning documents where the contacts property does not contain an item with a certain property.

I see in the RethinkDB examples they use a not() clause. I have browsed query.ex and cannot see any similar function to transform the contains to its negation.

Any help would be appreciated, thank you.

hamiltop commented 7 years ago

You have to use not_r, as not is part of Elixir.Kernel

pauliephonic commented 7 years ago

Okay, thanks very much for your help.