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

How to query a a value inside hasMany?

johnmadrigal_
last month
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

  • Jarrod
    Payload Team
    last month

    is relationTo and array or a string?

  • johnmadrigal_
    last month

    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"

  • jesschow
    Payload Team
    2 weeks 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
Can't find what you're looking for?
Get help straight from the Payload team with an Enterprise License.Learn More