Check if a key is defined

I would like to select all docs in my collection Message that have no key channel defined.

How can I do that?


My attempts so far:


fql`Message.all().where(x => !Object.hasPath(x, ['channel']))`

> expected value for object of type { *: Any }, received Doc


fql`Message.all().where(x => !x.channel)`

> The function ! does not exist on String


fql`Message.all().where(.channel)`

> expected type: Boolean, received String

If I try to run the check against keys that do not exist, an unexpected error occurs: Timeout during query merge · Issue #168 · fauna/fauna-js · GitHub

I believe you can check if the .field == null in the where clause, as it seems accessing a non-existent key will return null:

Collection.create({name: "test"})
>> SUCCESS
{
  name: "test",
  coll: Collection,
  ts: Time("2023-05-29T18:31:46.150Z"),
  indexes: {},
  constraints: []
}


test.create({foo: "one", bar: "two", baz: "three"})
>> SUCCESS
{
  id: "366085451881119812",
  coll: test,
  ts: Time("2023-05-29T18:32:23.080Z"),
  foo: "one",
  bar: "two",
  baz: "three"
}


test.create({foo: "one", bar: "two"})
>> SUCCESS
{
  id: "366085468532506692",
  coll: test,
  ts: Time("2023-05-29T18:32:38.950Z"),
  foo: "one",
  bar: "two"
}


test.where(.baz == null)
>> SUCCESS
{
  data: [
    {
      id: "366085468532506692",
      coll: test,
      ts: Time("2023-05-29T18:32:38.950Z"),
      foo: "one",
      bar: "two"
    }
  ]
}

Hey, @zvictor! I think Bindings is the solution you are looking for: Bindings - Fauna Documentation

With Bindings you can create an index based on some condition (wheter doc has some field or not in your case). Then you can easily get what you want from it.

@rcausey is correct here.

In javascript, there is an idea of “truthy-ness”. So when you check something like !x.channel the null value gets coerced to a boolean value.

In FQL, there is no “truthiness” and null cannot be coerced to a boolean in the same way it can in Javascript. So what is needed is to check explicitly if the value is null, e.g. .field == null

@sumerokr FQL v10 will not support index bindings, but we are working on filling in that functionality with computed fields that are defined directly on the Collection rather than indexes.

Oops, I missed the fact that the question was about v10. Thanks for the clarification!

Thank you all for the assistance!

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.