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.

How to query a a value inside hasMany?

default discord avatar
johnmadrigal_2 years ago
3

I have a field relationTo inside my collection that has hasMany. I'm trying to create a fetch query that can find a value inside of that field. How can I do that? Thanks

  • discord user avatar
    jarrod_not_jared
    2 years ago

    is relationTo and array or a string?

  • default discord avatar
    johnmadrigal_2 years ago

    It a news tags, this is the sample news return



    {


    newsTitle: "Hello",


    ....


    tags: [


    {


    value: {


    title: "Tag name",


    ....


    }


    relationTo: "tags"


    },


    {


    ....


    }


    ]


    }



    I want to query tags item title if its the same with "Tag name"

  • default discord avatar
    jessrynkar2 years ago
    @950202910095081473

    to clarify - you want to query your news collection and return documents based on the tags?



    That would look something like this:



      const posts = await payload.find({
        collection: 'posts',
        where: {
          'tags.name': {
            contains: 'Test', // Your tagname here
          },
        },
      });
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.