Querying by component field

System Information
  • Strapi Version: 3.6.2
  • Operating System: Ubuntu 20.04
  • Database: MongoDB
  • Node Version: v14.16.0
  • NPM Version: 6.14.11
  • Yarn Version: 0.32+git

Consider the following models and components:
Identity

...
{
  "kind": "collectionType",
  "collectionName": "identities",
  "info": {
    "name": "Identity",
    "description": ""
  },
  "attributes": {
    "externalId": {
      "type": "string"
    },
...
    "createdDate": {
      "type": "datetime"
    },
    "contact": {
      "type": "component",
      "repeatable": false,
      "component": "contact.contact"
    },
...

and this component:
contact:

...
{
  "collectionName": "components_contact_contacts",
  "info": {
    "name": "Contact",
    "icon": "address-card",
    "description": ""
  },
  "options": {},
  "attributes": {
    "email": {
      "type": "email"
    },
    "phoneNumber": {
      "type": "string"
    },
...

And here are some database records:

[
    {
        "role": "client",
        "plan": "basic",
        "status": "active",
        "_id": "60bef6b955444c0013a10816",
        "externalId": "johndoe",
        "published_at": "2021-06-08T04:48:57.236Z",
        "contact": {
            "_id": "60bef6b955444c0013a10817",
            "firstName": "John",
            "lastName": "Doe",
            "email": "jd@example.com",
            "address": "1 Yonge Street",
            "city": "Toronto",
            "postalCode": "",
            "country": "Canada",
            "phoneNumber": "8888888888",
            "__v": 0,
            "id": "60bef6b955444c0013a10817"
        },
        "createdAt": "2021-06-08T04:48:57.269Z",
        "updatedAt": "2021-06-08T04:48:57.301Z",
        "__v": 1,
        "id": "60bef6b955444c0013a10816"
    },
    {
        "role": "client",
        "plan": "basic",
        "status": "active",
        "_id": "60cb6024318e38070b3d7fb2",
        "externalId": "18",
        "published_at": "2021-06-17T14:45:56.908Z",
        "contact": {
            "_id": "60d10a641409c91e66600a5d",
            "firstName": "Jane",
            "lastName": "Doe",
            "email": "jd2@example.com",
            "address": "",
            "city": "",
            "postalCode": "",
            "country": "",
            "phoneNumber": "",
            "__v": 0,
            "id": "60d10a641409c91e66600a5d"
        },
        "createdAt": "2021-06-17T14:45:56.937Z",
        "updatedAt": "2021-06-21T21:53:40.463Z",
        "__v": 8
        },
        "id": "60cb6024318e38070b3d7fb2"
    },
...]

How do you query by the contact.email?

I’ve tried this query, and it doesn’t seem to work:

http://localhost:31337/identities?contact.email=jd@example.com

Response:

{

    "statusCode": 400,

    "error": "Bad Request",

    "message": "Your filters contain a field 'contact.email' that doesn't appear on your model definition nor its relations"

}

In the v3 it is not possible to filter on components, this is a feature we will be adding to the v4 though it will only work on components, not components within a dynamic zone.

1 Like

Hello @DMehaffy, components within a dynamic zone is really important, where DZ of Content types allows to add different type of components and we require to filter the content type based on the component name with in DZ. Struggling with this.

I understand recent V4 release is only having filter on just components but not on components with in a dynamic zone.

This will be very helpful and handy feature to have.

Thanks

We have no plans to support filtering inside of polymorphic relations and dynamic zones are polymorphic relations.

The complex logic required to do so simply beyond what we feel is needed right now. Performance and time required to actually implement this is not something we are willing to invest time into. Far more things with a higher priority.

(not saying it won’t be in the future, but right now and for the foreseeable future it’s something our engineering team has simply flat out said no to for now)

1 Like

Understood. Looking forward to those feature. Thank you.