首页 > 解决方案 > MYSQL JSON - 如何访问由数字键索引的嵌套 JSON 对象?

问题描述

我正在尝试提取由数字键索引的 JSON 文档子树。

我的 JSON 字符串:

{
    "pk": 20,
    "tree": {
        "100": {
            "values": [
                1, 2, 3
            ]
        },
        "abc" => 999
    }
}

我的代码:

$session = mysql_xdevapi\getSession("mysqlx://root:letmein@localhost");
$schema = $session->getSchema('test');
$coll = $schema->getCollection('myColl');
$expr = mysql_xdevapi\Expression('$.tree.*');
$result = $coll->find('$.pk=20')->fields(['$.tree[100]'])->execute();

使用'$.tree[100]'结果

    [
        'tree' => null
    ]

使用'$.tree.*'结果

    [
        'tree' => [
            0 => [
                1, 2, 3
            ],
            1 => 999
        ]
    ]

使用'$.tree.abc'结果

    [
        'tree' => [
            'abc' => 999
        ]
    ]

所以,'$.tree.abc'有效,但'$.tree[100]'没有。

问题。如何values使用 '$.tree[100]' 表达式访问密钥?

谢谢!

标签: phpmysqljsonmysql-8.0document-store

解决方案


Thnx 报告,以下情况:

$expr = mysql_xdevapi\Expression('$.tree."100"'); 
$result = $coll->find('$.pk=25')->fields($expr)->execute();

计划于 10 月 14 日推出的 mysql_xdevapi v8.0.18 将支持。


推荐阅读