alasql JSON 查询 - 限制还是用户错误?无法查询数组内但未出现在第一个位置的项

alasql JSON Query - Limitation or User error? Unable to query an item which is inside an array but when not present on first position

提问人:Anx 提问时间:12/8/2022 最后编辑:Anx 更新时间:1/18/2023 访问量:106

问:

我刚刚发现了 alasql,它很棒!但是,我不确定这是否是已知限制或查询的问题,但由于某种原因,我无法使用下面的查询在下面的 JSON 中找到值“algb2b-gb”。看起来,如果未提及数组中项的确切位置,则查询无法给出结果。这是一个已知的限制,还是有更好的方法来查询数组项?

欣赏你的想法。

下面是 SQL 查询:

SELECT COUNT( * ) FROM ? WHERE profile->ad_site_permissions->0 = "algb2b-gb" 

下面是我的JSON

    [{
        "id": "00u1fcghdvmRWnNb81d8",
        "profile": {
            "lastName": "AUvalidation",
            "ad_site_permissions": [
                "algb2b-us",
                "algb2b-it",
                "algb2b-de",
                "algb2b-au",
                "algb2b-nz",
                "algb2b-nl",
                "algb2b-fr",
                "algb2b-gb",
                "algb2b-es",
                "algb2b-pr",
                "algb2b-ca",
                "algb2b-br",
                "algb2b-ch"
            ],
            "ad_system_permissions": [
                "bpReadAccess",
                "ecomReadAccess"
            ]


        }
    }, {
        "id": "00u2zvh77sVZMACHYLIZ",
        "profile": {
            "ad_site_permissions": [
                "algb2b-ca",
                "algb2b-fr",
                "algb2b-it",
                "algb2b-de",
                "algb2b-nl",
                "algb2b-au",
                "algb2b-nz",
                "algb2b-gb",
                "algb2b-es",
                "algb2b-br"
            ],
            "ad_system_permissions": [
                "bpReadAccess",
                "ecomReadAccess"
            ]
        }
    }]

我的输出是

[
    {
        "COUNT(*)": 0
    }
]

但是,如果我将查询更改为下面,那么它可以工作:

SELECT COUNT( * ) FROM ? WHERE profile->ad_site_permissions->7 = "algb2b-gb" 

转到 github 站点进行文档访问。

JavaScript 数组 JSON alasql

评论


答:

1赞 Ambuj sahu 1/18/2023 #1
SELECT COUNT( * ) FROM ? WHERE profile->ad_site_permissions->0 = "algb2b-gb"`

上面的查询将检查数组项的计数 它是 ALGB2B-US,它不等于 ALGB2B-GB。因此,计数为 0。ad_site_permissions[0]

您可以优化查询以搜索数组的所有索引,例如:

var data = [{a:[1,2,3,4]}, {a:[6,7,8,9]}]

var response = alasql(`SELECT COUNT( * ) FROM ? WHERE 6 = any(a)`,[data]);

console.log(response) // [ { 'COUNT(*)': 1 } ]

所以你可以尝试这样的事情

 var res = alasql(`SELECT COUNT( * ) FROM ? WHERE  "algb2b-gb" = any(profile->ad_site_permissions) `, [data]);

console.log(res) // [ { 'COUNT(*)': 2 } ]

评论

0赞 Anx 1/20/2023
伟大!谢谢你的提示。我现在可以标记问题结束了。