1

I am trying to extract JSON document subtree that's indexed by numerical key.

My JSON string:

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

My code:

$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();

Using '$.tree[100]' results in

    [
        'tree' => null
    ]

Using '$.tree.*' results in

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

Using '$.tree.abc' results in

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

So, '$.tree.abc' works, but '$.tree[100]' doesnt.

Question. How can I access values key using '$.tree[100]' expression?

Thanks!

5
  • Try: ... $coll->find('$.pk=20')->fields(['$.tree."100"'])->execute();. Commented Jul 23, 2019 at 17:37
  • nope: mysql_xdevapi\CollectionFind::fields(): Error while parsing, details: CDK Error: After seeing '$.tree."100" AS ', looking at '100': Expected identifier after AS (cdk:8). How would you do that search from Javascript mysqlsh? Commented Jul 23, 2019 at 17:43
  • Filed a bug report: bugs.php.net/bug.php?id=78331. Commented Jul 24, 2019 at 15:52
  • This works for me from mysqlsh-js: mysqlsh-js> db.myColl.find("pk = 20").fields("tree.'100'");. Commented Jul 24, 2019 at 20:57
  • Yes, it does. But only from mysqlsh. There's a bug in PHP extension. Commented Jul 24, 2019 at 21:42

1 Answer 1

2

Thnx for report, following case:

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

will be supported in mysql_xdevapi v8.0.18 which is planned for Oct 14.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.