Return first non-null value

Hi all

Is there a native FQL function that will return the first non-null value? Similar to Coalesce in SQL? I have a few variables set using Let and I want to find the first that is non-nullable. I suppose I could put them all in an array and use a combination of Filter and Select but wondered if there was an easier way?

Cheers!

Nulls sort last, unless you set reverse: true: Data types - Fauna Documentation

Check the first value returned, if it is null, there are no non-null values to return, otherwise, there you go.

ok cool, I thought this would probably be the case. Was hoping there was something built in, but that’s fine, this will work just fine!

Sort comes from using sets, such as an index. That won’t help you use values in a Let expression. As you noted, you’d need to use Select and Filter:

> Let(
  {
    a: null,
    b: null,
    c: "a value!",
    d: null
  },
  Select(
    0,
    Filter(
      [Var("a"), Var("b"), Var("c"), Var("d")],
      Lambda("val", Not(IsNull(Var("val"))))
    )
  )
)
'a value!'

But if you can rework your query to use an index, you’ll be able to find the first non-null value for any number of values.