entityService filters with relation data v4

I know this is an old post but I wanted to clarify because I was about to ask the same question and didn’t see a specific answer other than the documentation link, which do not mention the combination of filtering and populating.

Filtering related fields does work, but the syntax is a bit different from what the OP assumed. Dont write the property path as string, but write it as a nested object instead.
e.g. author: { id: user.id } instead of author.id: user.id

So, using Query Engine, the code should be like this:

const entries = await strapi.query('api::article.article').findMany({
  populate: ['author', 'reviewers'],
  where: {
    $or: [
      { author: { id: user.id }},
      { reviewers: { user: { id: user.id }}},
    ],      
  },
});

Using Entity Service, the code should be like this:

const entries = await strapi.entityService.findMany('api::article.article', {
  populate: ['author', 'reviewers'],
  filters: {
    $or: [
      { author: { id: user.id }},
      { reviewers: { user: { id: user.id }}},
    ],  		
  },
});

Also, the filtering will work even without specifically populating the relations.

2 Likes