【问题标题】:MYSQL JSON - how do I access nested JSON object that's indexed by numerical key?MYSQL JSON - 如何访问由数字键索引的嵌套 JSON 对象?
【发布时间】:2019-12-01 19:18:39
【问题描述】:

我正在尝试提取由数字键索引的 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]' 无效。

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

谢谢!

【问题讨论】:

  • 试试:... $coll->find('$.pk=20')->fields(['$.tree."100"'])->execute();.
  • nope: mysql_xdevapi\CollectionFind::fields(): Error while parsing, details: CDK Error: After seen '$.tree."100" AS', 查看 '100': Expected identifier after作为(cdk:8)。您将如何从 Javascript mysqlsh 进行搜索?
  • 提交了错误报告:bugs.php.net/bug.php?id=78331.
  • 这适用于 mysqlsh-js:mysqlsh-js> db.myColl.find("pk = 20").fields("tree.'100'");
  • 是的,确实如此。但仅来自 mysqlsh。 PHP 扩展中存在错误。

标签: php mysql json mysql-8.0 document-store


【解决方案1】:

感谢报告,以下案例:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-04
    • 1970-01-01
    • 2015-08-13
    • 2017-11-27
    相关资源
    最近更新 更多