【发布时间】: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