You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Strapi API请求中基于多选数组的过滤问题求助

Strapi API数组字段过滤解决方案

目标数据

{
  "data": [
    {
      "id": 2,
      "attributes": {
        "selectTheTitleView": "Column_3_View",
        "selectUserRoles": [ "ALL" ],
        "selectSubChannels": [ "Elite" ]
      }
    },
    {
      "id": 5,
      "attributes": {
        "selectTheTitleView": "Column_3_View",
        "selectUserRoles": [ "TM-SUPERVISOR" ],
        "selectSubChannels": [ "Sparc" ]
      }
    },
    {
      "id": 4,
      "attributes": {
        "selectTheTitleView": "Column_3_View",
        "selectUserRoles": [ "BRM-SUPERVISOR", "TM-SUPERVISOR", "VERTICAL-HEAD-SUPERVISOR" ],
        "selectSubChannels": [ "Sparc" ]
      }
    },
    {
      "id": 3,
      "attributes": {
        "selectTheTitleView": "Column_3_View",
        "selectUserRoles": [ "PRODUCER" ],
        "selectSubChannels": [ "S2S" ]
      }
    }
  ],
  "meta": {
    "pagination": {
      "page": 1,
      "pageSize": 25,
      "pageCount": 1,
      "total": 4
    }
  }
}

需求

获取所有selectUserRoles数组中包含"PRODUCER"的目录数据。

错误尝试的请求

  • http://localhost:1337/api/supplier-catalogs?filters/[selectUserRoles][$in][0]=PRODUCER
  • http://localhost:1337/api/supplier-catalogs?filters/[selectUserRoles][$in]=PRODUCER
  • http://localhost:1337/api/supplier-catalogs?filters/[selectUserRoles][$eq]=PRODUCER

正确的请求格式

针对Strapi中数组字段包含指定值的场景,应使用$contains操作符,正确请求URL如下:

http://localhost:1337/api/supplier-catalogs?filters[selectUserRoles][$contains]=PRODUCER

也可使用URL编码格式(适配特殊字符场景):

http://localhost:1337/api/supplier-catalogs?filters%5BselectUserRoles%5D%5B%24contains%5D=PRODUCER

原理说明

  • $in用于匹配单个值字段等于给定数组中的任意值,不适用于数组类型字段;
  • $eq是精确匹配,要求字段值与给定值完全一致,无法匹配数组字段;
  • $contains专门用于判断数组字段是否包含指定单个值,完全匹配你的需求。

内容的提问来源于stack exchange,提问作者aman

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 19:27:16