gpt4 book ai didi

javascript - 纯javascript(es2015)函数从树json结构中提取子元素

转载 作者:行者123 更新时间:2023-12-03 05:12:10 30 4
gpt4 key购买 nike

我有一个 DocumentDb 数据库,其中文档类型之一具有以下结构:

{
"structure": {
"id": "7d2d5d3f-0c82-4910-aa0b-54d8067588a3",
"unittype": 1,
"name": "MyCompany",
"language": "nb-NO",
"managers": [],
"logoURL": null,
"orginfo": {
"Orgnumber": null,
"Country": null,
"Sector": null,
"Code": null
},
"tags": [],
"respondents": [],
"childUnits": [
{
"id": "3fb44416-8fa8-4b60-8c0c-03b333d176f7",
"unittype": 3,
"name": "Marketing",
"language": "nb-NO",
"managers": [],
"logoURL": null,
"orginfo": {
"Orgnumber": null,
"Country": null,
"Sector": null,
"Code": null
},
"tags": [],
"respondents": [],
"childUnits": [
{
"id": "49932d6f-518e-4511-9bc9-f6f747a81968",
"unittype": 3,
"name": "Internet",
"language": "nb-NO",
"managers": [],
"logoURL": null,
"orginfo": {
"Orgnumber": null,
"Country": null,
"Sector": null,
"Code": null
},
"tags": [],
"respondents": [],
"childUnits": [],
"Parent": "3fb44416-8fa8-4b60-8c0c-03b333d176f7"
},
{
"id": "aa76010d-ae59-49b1-b929-9572eb536cc6",
"unittype": 3,
"name": "DM",
"language": "nb-NO",
"managers": [],
"logoURL": null,
"orginfo": {
"Orgnumber": null,
"Country": null,
"Sector": null,
"Code": null
},
"tags": [],
"respondents": [],
"childUnits": [],
"Parent": "3fb44416-8fa8-4b60-8c0c-03b333d176f7"
}
],
"Parent": "7d2d5d3f-0c82-4910-aa0b-54d8067588a3"
},
{
"id": "be1d142a-e09d-4828-aceb-07d65321da5d",
"unittype": 3,
"name": "Support",
"language": "nb-NO",
"managers": [],
"logoURL": null,
"orginfo": {
"Orgnumber": null,
"Country": null,
"Sector": null,
"Code": null
},
"tags": [],
"respondents": [],
"childUnits": [],
"Parent": "7d2d5d3f-0c82-4910-aa0b-54d8067588a3"
}
],
"Parent": null
},
"id": "07b4c7c5-7324-4eef-9e4e-9deb70615ec4",
"type": "organization",
"owner": "auth0|571f2eb34247998a66726b02",
"public": false,
"_rid": "GPFbALqREAMNAAAAAAAAAA==",
"_self": "dbs/GPFbAA==/colls/GPFbALqREAM=/docs/GPFbALqREAMNAAAAAAAAAA==/",
"_etag": "\"00004707-0000-0000-0000-5881cef40000\"",
"_attachments": "attachments/",
"_ts": 1484902132
}

这是旧系统的重建,其中所有内容都存储在关系数据库中,每个 Unit(childUnits 属性中的对象)最初是表中的一行。我们与我们最大的客户之一进行了测试,最终得到了一个拥有 500 多个单元的结构。超过 3000 名受访者。 (我没有在此演示中添加任何内容,但 Respondent 对象包含电子邮件(必需)和可能的手机号码。)

结果并不好。在我的(相当强大的)devpc 上本地运行,结构检索大约需要 2-3 秒。并且 DocumentDb 不支持文档部分的检索。因此,每次用户单击 TreeView 中的一个单元时,整个结构都会通过线路重新发送(这部分我可能可以缓存,但如果用户更改某些内容,我将必须存储整个内容,然后读回) .

然而,DocumentDb 支持的是 Javascript 存储过程,它使用 Chakra,据我所知,它符合 ES2015。

所以我的想法是创建一个存储过程,向其中提供文档(组织)的 id 和要检索的单元的 id 。它会搜索 json 文档并检索我想要的单元并将其发送回。

不使用外部库,仅使用 ES2015 功能是否可以实现这一点?

最佳答案

提高性能的最佳方法是利用文档内 JOIN - https://www.documentdb.com/sql/demo#JOIN

假设结构属性实际上是文档的一部分,JOIN 查询将如下所示:

SELECT c.id, childUnit 
FROM c JOIN childUnit IN c.structure.childUnits
WHERE c.structure.id = "7d2d5d3f-0c82-4910-aa0b-54d8067588a3"
AND childUnit.id = "3fb44416-8fa8-4b60-8c0c-03b333d176f7"*

如果结构属性实际上不是原始文档的一部分,则 JOIN 查询将如下所示:

SELECT c.id, childUnit 
FROM c JOIN childUnit IN c.childUnits
WHERE c.id = "7d2d5d3f-0c82-4910-aa0b-54d8067588a3"
AND childUnit.id = "3fb44416-8fa8-4b60-8c0c-03b333d176f7"*

此类查询将仅返回文档中指定的子单元。 WHERE 过滤器中的unit idchildUnit id 可提高查询性能。

关于javascript - 纯javascript(es2015)函数从树json结构中提取子元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41759640/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com