Querying a number field that is `null`

default discord avatar
itsjxck
7 months 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

  • discord user avatar
    jesschow
    Payload Team
    6 months 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,
              },
            }
          ]
        },
  • default discord avatar
    itsjxck
    6 months 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
Like what we're doing?
Star us on GitHub!

Star

Connect with the Payload Community on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.