MySQL从存储的json文档中提取多个嵌套值作为一个值

时间:2018-05-15 18:23:34

标签: mysql json mysql-json

我的最终目标是构建一个JSON_OBJECT()选择语句,该语句只从存储的json值中收集几个值。

我当前对select语句的迭代:

SELECT
  JSON_OBJECT(
      'id', document->>'$.id',
      'slug', document->>'$.slug',
      'title', document->>'$.title',
      'date_published', document->>'$.date_published',
      'toc', (SELECT JSON_OBJECT('title', document->>'$.toc[*][*].title') WHERE document->>'$.id' = document->>'$.id' )
  ) as document
FROM documents
WHERE 
  `type` = "item" AND
  NOW() > date_published;

示例item文档可能是:

{
  "id": "d434089d-33ac-11e8-af1c-005056a40c60",
  "toc": [
    [
      {
        "id": "d4e1e442-57a9-11e8-af1c-005056a40c60",
        "slug": "0-intro",
        "chunk": 0,
        "index": 0,
        "pages": [],
        "title": "Intro",
        "urlsuffix": ""
      },
      {
        "id": "d4e1ef07-57a9-11e8-af1c-005056a40c60",
        "slug": "1-beginning",
        "chunk": 0,
        "index": 1,
        "pages": [
          {
            "id": "d4e1f726-57a9-11e8-af1c-005056a40c60",
            "slug": "2-nested-example",
            "chunk": 0,
            "index": 2,
            "pages": [],
            "title": "Nested Example",
            "urlsuffix": ""
          },
          {
            "id": "d4e1fee4-57a9-11e8-af1c-005056a40c60",
            "slug": "3-three-layers-deep",
            "chunk": 0,
            "index": 3,
            "pages": [
              {
                "id": "d4e2065e-57a9-11e8-af1c-005056a40c60",
                "slug": "4-four-in",
                "chunk": 0,
                "index": 4,
                "pages": [],
                "title": "Four In",
                "urlsuffix": ""
              },
              {
                "id": "d4e20e47-57a9-11e8-af1c-005056a40c60",
                "slug": "5-maximum-nesting",
                "chunk": 0,
                "index": 5,
                "pages": [],
                "title": "This is likely the maximum amount of Nesting",
                "urlsuffix": ""
              }
            ],
            "title": "Three Layers Deep",
            "urlsuffix": ""
          },
          {
            "id": "d4e2168b-57a9-11e8-af1c-005056a40c60",
            "slug": "6-follow-up-chapter",
            "chunk": 0,
            "index": 6,
            "pages": [],
            "title": "Follow-up Chapter",
            "urlsuffix": ""
          }
        ],
        "title": "The Beginning",
        "urlsuffix": ""
      }
    ]
  ],
  "slug": "an-item",
  "tags": [
    "test-item"
  ],
  "type": "item",
  "title": "An Item",
  "download": null,
  "date_added": "1522342602",
  "page_count": null,
  "description": "",
  "date_published": "1522238400"
}

这就是我试图完成的select语句的结果记录(对于示例文档):

{
  "id": "d434089d-33ac-11e8-af1c-005056a40c60",
  "slug": "an-item",
  "title": "An Item",
  "date_published": "1522238400",
  "toc": [
    {
      "id": "d4e1e442-57a9-11e8-af1c-005056a40c60",
      "slug": "0-intro",
      "title": "Intro",
      "urlsuffix": ""
    },
    {
      "id": "d4e1ef07-57a9-11e8-af1c-005056a40c60",
      "slug": "1-beginning",
      "title": "The Beginning",
      "urlsuffix": ""
    }
  ]
}

所以基本上,只是一个顶级目录项目的数组(深度[1])

如果我绝对必须,我将承认完成应用程序中的逻辑,我只是​​觉得必须有一种方法来使用MySQL json函数。

选择对象/阵列时有没有办法要求最大深度?

我正在使用5.7.22,所以我确实拥有最新的花式json功能(JSON_ARRAYAGG / JSON_OBJECTAGG等)

0 个答案:

没有答案