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

如何通过ID以外的字段查询Strapi集合类型?

Strapi REST API 按非ID字段查询的解决方案
  • 用find接口配合过滤条件,追加pagination[pageSize]=1参数,强制返回匹配的第一条数据,效果和findOne一致:
    https://my.strapi.host:1337/api/my-collection?filters[MyOtherField][$eq]=lgNKlCLaoe2ziYqPhlJS3sNJZaRkRlcNJp38&pagination[pageSize]=1
    
  • 给目标字段设置唯一约束:如果MyOtherField是唯一值,在Strapi后台的集合类型字段设置里开启唯一校验,这样过滤后只会返回一条结果,不会出现多条匹配的情况。
  • 自定义路由与控制器:在Strapi项目中新增自定义路由,比如定义/api/my-collection/by-other-field/:value,然后在对应控制器中编写查询逻辑,直接调用findOne方法指定非ID字段查询:
    // 控制器示例代码
    async findByOtherField(ctx) {
      const { value } = ctx.params;
      const entry = await strapi.db.query('api::my-collection.my-collection').findOne({
        where: { MyOtherField: value },
      });
      ctx.body = entry;
    }
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.15 06:52:44