如何使用 GROQ 查询 Sanity 中对象数组中引用的文档字段?

use*_*377 3 sanity groq

我有一个文档,其中包含一组对象,其中一个字段是对另一个文档的引用。以下查询仅返回引用的文档 _id 和 _type,我需要这些文档中的其他字段。

// GROQ query
*[slug.current == $slug]{
  title,
  slug,
  objectArray
}
Run Code Online (Sandbox Code Playgroud)

这导致:

"result": [
0: {
  "title": "Test Document"
  "slug": {
    "_type": "slug"
    "current": "test-document"
    }
  "objectArray": [
    0: {...}
    1: {
      "_key": "583ec1dee738"
      "_type": "documentObject"
      "objectTitle" : "Test Object"
      "documentReference": {
        "_ref": "2f9b93b4-4924-45f2-af72-a38f7d9ebeb4"
        "_type": "reference"
        }
      "booleanField": true
      }
    ]
  }
]
Run Code Online (Sandbox Code Playgroud)

documentReference在模式中有自己的一组字段(即标题),我需要在查询中返回这些字段。

我该怎么做呢?

我已经查看了连接对象投影中的 Sanity 文档,但是当引用位于对象数组中时,我无法获得正确的语法。

Ale*_*ubo 5

您需要对引用进行连接:

\n
*[slug.current == $slug] {\n  title,\n  slug,\n  objectArray[] {\n    documentReference->\n  }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

语法objectArray[]\xc2\xa0 可以被认为是“对于每个元素”,并->执行连接来查找引用的文档。其他

\n

  • 您可以尝试使用“objectArray[]{...,documentReference->}”代替“objectArray[]->”吗? (4认同)