Like what we’re doing? Star us on GitHub!

Querying a number field that is `null`

itsjxck
4 weeks ago
4

I have a number field which is optional, and if set and unset gets a

null

value. If I use:


{
  or: [
    {
      numberField: {
        exists: false,
      },
    },
    {
      numberField: {
        equals: null,
      },
    },
  ],
}

it does not match the field.



Is this a bug or is there another way to check if a field specifically has a

null

value? My assumption was that

null

would still match the

exists: false

query

  • jesschow
    Payload Team
    2 weeks ago

    hi @itsjxck - I replicated your query and it worked correctly for me. Exists>false returned the same docs as equals>null.



    To get the docs that have

    numberField: null

    and not the ones where it is undefined, I used:


    where: {
          and: [
            {
              numberField: {
                equals: null,
              },
            },
            {
              numberField: {
                exists: true,
              },
            }
          ]
        },
  • itsjxck
    2 weeks ago

    @jesschow thanks for that; I can confirm it does indeed work when doing this in payload, but in a REST query, it does not work



    I assume this is because

    null

    is not a valid query parameter value



    This behaviour seems strange though, I would expect that a field that has it's values removed, would have the same

    undefined

    value as if the field had never had the value set in the first place

Open the post
Continue the discussion in Discord
Can't find what you're looking for?
Get help straight from the Payload team with an Enterprise License.Learn More