How to query a a value inside hasMany?

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

  • default discord avatar
    Jarrod
    7 months ago

    is relationTo and array or a string?

  • default discord avatar
    johnmadrigal_
    7 months 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"

  • discord user avatar
    jesschow
    Payload Team
    6 months ago

    @johnmadrigal_ 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
          },
        },
      });
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.