Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

Query Non-Empty Array Fields in Payload CMS Version 3

default discord avatar
zed0547last year
8

Hey

@691568994246983690

,



Can you try this:


const result = await payload.find({
  collection: 'posts',
  where: {
    scheduledActions: { equals: [] },
  },
  limit: 10000,
  depth: 0,
})


I can't remember exactly the way to do this but we can figure it out here with a few trial and errors

  • default discord avatar
    dkirchhoflast year

    "not_equals: []" seems to work. Have a try on documents, where the array is not yet defined.

  • default discord avatar
    zed0547last year

    OH, sorry you mentioned

    "Not Equals"

    and I read this as "equals" accidentally



    My mistake, but glad you figured it out in the end

  • default discord avatar
    ritsu0455last year

    This works ^ because MongoDB can accept that value, generally, for now there's no any payload "official" way that works in all database adapters and guaranteed to not break in the future

  • default discord avatar
    zed0547last year

    I guess an adapter agnostic way would be a distinct operator here?

  • default discord avatar
    ritsu0455last year

    Maybe something like

    scheduledActions._length: { equals: 0 }
  • default discord avatar
    dkirchhoflast year

    Since I use mongodb, im fine with the specific solution. Thank you.


    Just wondering, why the "old syntax" doesn't work anymore.



    @423216344302092288

    scheduledActions._length throws the same error

  • default discord avatar
    ritsu0455last year
    scheduledActions._length

    is just my imagination for a possible in the future official way to do this



    The old syntax doesn't work anymore probably because we expect

    0

    as a field defined in the payload config now

  • default discord avatar
    dkirchhoflast year

    Got it. Thanks again 🙂

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.