neo4j - 生成嵌套递归 json 的密码请求
问题描述
到目前为止,我已经创建了以下节点:
CREATE
(b:Brand {identifier: 'ANTIINFECTIEUX',brand:'ANTIINFECTIEUX',description :'ANTIINFECTIEUX' }),
(b)<-[:IS_A_CATALOG_BELONGING_TO]-(c:Catalog {identifier: 'ANTIBACTERIENS',catalogName:'ANTIBACTERIENS',brand:'ANTIINFECTIEUX',brandIdentifier:'ANTIINFECTIEUX',description :'ANTIBACTERIENS'}),
(c)<-[:IS_A_PRODUCT_BELONGING_TO_THAT_CATALOG]-(p:Product {identifier: 'Amikacine',productName:'Amikacine',brand:'ANTIINFECTIEUX',brandIdentifier:'ANTIINFECTIEUX',catalog:'ANTIBACTERIENS',catalogIdentifier:'ANTIBACTERIENS',description :'Amikacine',DCI:'Amikacine', Dosage:'1g', Forme:'Inj', Case:'false', Poste:'false', CTRE:'false', CHR:'true', CHN:'true', Reference:'Amiklin', price:'200.75', stock:'500',NumeroDeLot:'87',CodeBarre:'87878787878787878',QuantiteDemandee:'50'}),
(p)-[:IS_A_PRODUCT_BELONGING_TO_THAT_BRAND]->(b),
(c)<-[:IS_A_PRODUCT_BELONGING_TO_THAT_CATALOG]-(p2:Product {identifier: 'Amoxicilline',productName:'Amoxicilline',brand:'ANTIINFECTIEUX',brandIdentifier:'ANTIINFECTIEUX',catalog:'ANTIBACTERIENS',catalogIdentifier:'ANTIBACTERIENS',description :'Amoxicilline',DCI:'Amoxicilline', Dosage:'500mg', Forme:'Gélules', Case:'false', Poste:'true', CTRE:'true', CHR:'true', CHN:'true', Reference:'Clamoxyl', price:'250.75', stock:'700',NumeroDeLot:'8777',CodeBarre:'998898979797',QuantiteDemandee:'50'}),
(p2)-[:IS_A_PRODUCT_BELONGING_TO_THAT_BRAND]->(b),
(b1:Brand {identifier: 'ANESTHESIQUES',brand:'ANESTHESIQUES',description :'ANESTHESIQUES' }),
(b1)<-[:IS_A_CATALOG_BELONGING_TO]-(c1:Catalog {identifier: 'Anesthesiques_Generaux_et_Gaz_medicaux',catalogName:'Anesthesiques_Generaux_et_Gaz_medicaux',brand:'ANESTHESIQUES',brandIdentifier:'ANESTHESIQUES',description :'Anesthésiques généraux et Gaz médicaux'}),
(c1)<-[:IS_A_PRODUCT_BELONGING_TO_THAT_CATALOG]-(p1:Product {identifier: 'Kétamine',productName:'Kétamine',brand:'ANESTHESIQUES',brandIdentifier:'ANESTHESIQUES',catalog:'Anesthesiques_Generaux_et_Gaz_medicaux',catalogIdentifier:'Anesthesiques_Generaux_et_Gaz_medicaux',description :'Kétamine',DCI:'Kétamine', Dosage:'50mg/amp', Forme:'Inj', Case:'false', Poste:'false', CTRE:'true', CHR:'true', CHN:'true', Reference:'Kétalar', price:'900.75', stock:'300',NumeroDeLot:'677',CodeBarre:'5454578788',QuantiteDemandee:'10'}),
(p1)-[:IS_A_PRODUCT_BELONGING_TO_THAT_BRAND]->(b1)
目的是获得这样的嵌套 json:
{
"menuItems":[
{
"name":"Anesthesiques_Generaux_et_Gaz_medicaux",
"children":[
{
"name":"ANESTHESIQUES",
"children":[
{
"name":"Kétamine",
"ProductCHR":"true",
"ProductForme":"Inj",
"ProductCHN":"true",
"ProductReference":"Kétalar",
"ProductDCI":"Kétamine",
"ProductCase":"false",
"ProductDosage":"50mg/amp",
"ProductIdentifier":"Kétamine",
"ProductPoste":"false",
"ProductPrice":"900.75",
"ProductCTRE":"true",
"ProductStock":"300"
}
]
}
]
},
{
"name":"ANTIBACTERIENS",
"children":[
{
"name":"ANTIINFECTIEUX",
"children":[
{
"name":"Amikacine",
"ProductCHR":"true",
"ProductForme":"Inj",
"ProductCHN":"true",
"ProductReference":"Amiklin",
"ProductDCI":"Amikacine",
"ProductCase":"false",
"ProductDosage":"1g",
"ProductIdentifier":"Amikacine",
"ProductPoste":"false",
"ProductPrice":"200.75",
"ProductCTRE":"false",
"ProductStock":"500"
},
{
"name":"Amoxicilline",
"ProductCHR":"true",
"ProductForme":"Gélules",
"ProductCHN":"true",
"ProductReference":"Clamoxyl",
"ProductDCI":"Amoxicilline",
"ProductCase":"false",
"ProductDosage":"500mg",
"ProductIdentifier":"Amoxicilline",
"ProductPoste":"true",
"ProductPrice":"250.75",
"ProductCTRE":"true",
"ProductStock":"700"
}
]
}
]
}
]
}
为了生成该 JSON 文件,我提出了以下密码请求:
MATCH (Brand:Brand)
OPTIONAL MATCH (Brand)<-[:IS_A_CATALOG_BELONGING_TO]-(Catalog:Catalog)
OPTIONAL MATCH (Catalog)<-[:IS_A_PRODUCT_BELONGING_TO_THAT_CATALOG]-(Product:Product)
OPTIONAL MATCH (Product)-[:IS_A_PRODUCT_BELONGING_TO_THAT_BRAND]->(Brand)
WITH Brand, Catalog, Product
ORDER BY Product.identifier ASC
WITH Brand, Catalog,
collect({name: Product.DCI, ProductIdentifier:Product.identifier,ProductDCI:Product.DCI,
ProductDosage:Product.Dosage,ProductForme:Product.Forme,ProductCase:Product.Case,
ProductPoste:Product.Poste,ProductCTRE:Product.CTRE,ProductCHR:Product.CHR,ProductCHN:Product.CHN,
ProductReference:Product.Reference,ProductPrice:Product.price,ProductStock:Product.stock,
NumeroDeLot:Product.NumeroDeLot,CodeBarre:Product.CodeBarre,QuantiteDemandee:Product.QuantiteDemandee}) AS pNames
ORDER BY Catalog.identifier ASC
WITH Brand.identifier AS name,
collect(Catalog.identifier ) AS cname,
collect( pNames) AS children
ORDER BY name ASC
RETURN apoc.convert.toJson({name:name,cname:cname,children:children})
该请求包含所有需要的信息,但不幸的是,理想的输出形式与我的期望(我真正想要的结果 json)不符,正如您在此处看到的那样。
"{
"children":[
[
{
"NumeroDeLot":"677",
"ProductReference":"Kétalar",
"ProductCase":"false",
"ProductPrice":"900.75",
"ProductCTRE":"true",
"QuantiteDemandee":"10",
"ProductCHR":"true",
"ProductForme":"Inj",
"ProductCHN":"true",
"ProductDCI":"Kétamine",
"ProductDosage":"50mg/amp",
"ProductIdentifier":"Kétamine",
"name":"Kétamine",
"ProductPoste":"false",
"CodeBarre":"5454578788",
"ProductStock":"300"}
]
],
"name":"ANESTHESIQUES",
"cname":["Anesthesiques_Generaux_et_Gaz_medicaux"]
}
"
"{
"children":[
[
{
"NumeroDeLot":"87",
"ProductReference":"Amiklin",
"ProductCase":"false",
"ProductPrice":"200.75",
"ProductCTRE":"false",
"QuantiteDemandee":"50",
"ProductCHR":"true",
"ProductForme":"Inj",
"ProductCHN":"true",
"ProductDCI":"Amikacine",
"ProductDosage":"1g",
"ProductIdentifier":"Amikacine",
"name":"Amikacine",
"ProductPoste":"false",
"CodeBarre":"87878787878787878",
"ProductStock":"500"
},
{
"NumeroDeLot":"8777",
"ProductReference":"Clamoxyl",
"ProductCase":"false",
"ProductPrice":"250.75",
"ProductCTRE":"true",
"QuantiteDemandee":"50",
"ProductCHR":"true",
"ProductForme":"Gélules",
"ProductCHN":"true",
"ProductDCI":"Amoxicilline",
"ProductDosage":"500mg",
"ProductIdentifier":"Amoxicilline",
"name":"Amoxicilline",
"ProductPoste":"true",
"CodeBarre":"998898979797",
"ProductStock":"700"
}
]
],
"name":"ANTIINFECTIEUX",
"cname":["ANTIBACTERIENS"]
}"
如果有人可以帮助我实现这一目标,那就太好了。
{
"menuItems":[
{
"name":"Brand Name1",
"children":[
{
"name":"Catalog Name",
"children":[
{
"name": "productName1",
.....................
etc
},
{
"name": "productName2"
.....................
},
........................
{
"name": "productNameN"
.....................
}
]
},
......................................
{
"name":"Catalog NameN",
"children":[
{
"name": "productName1ForCatalogNameN",
.....................
etc
},
{
"name": "productName2ForCatalogNameN"
.....................
},
........................
{
"name": "productNameNForCatalogNameN"
.....................
}
]
},
]
},
....................................................................
{
"name":"Brand NameN",
"children":[
{
"name":"Catalog Name",
"children":[
{
"name": "productName1",
.....................
etc
},
{
"name": "productName2"
.....................
},
........................
{
"name": "productNameN"
.....................
}
]
},
......................................
{
"name":"Catalog NameN",
"children":[
{
"name": "productName1ForCatalogNameN",
.....................
etc
},
{
"name": "productName2ForCatalogNameN"
.....................
},
........................
{
"name": "productNameNForCatalogNameN"
.....................
}
]
},
]
}
]
}
非常感谢。
解决方案
CREATE
(b:Brand {name: 'ANTIINFECTIEUX',brand:'ANTIINFECTIEUX',description :'ANTIINFECTIEUX' }),
(b)<-[:IS_A_CATALOG_BELONGING_TO]-(c:Catalog {name: 'ANTIBACTERIENS',catalogName:'ANTIBACTERIENS',brand:'ANTIINFECTIEUX',brandIdentifier:'ANTIINFECTIEUX',description :'ANTIBACTERIENS'}),
(c)<-[:IS_A_PRODUCT_BELONGING_TO_THAT_CATALOG]-(p:Product {name: 'Amikacine',productName:'Amikacine',brand:'ANTIINFECTIEUX',brandIdentifier:'ANTIINFECTIEUX',catalog:'ANTIBACTERIENS',catalogIdentifier:'ANTIBACTERIENS',description :'Amikacine',DCI:'Amikacine', Dosage:'1g', Forme:'Inj', Case:'false', Poste:'false', CTRE:'false', CHR:'true', CHN:'true', Reference:'Amiklin', price:'200.75', stock:'500',NumeroDeLot:'87',CodeBarre:'87878787878787878',QuantiteDemandee:'50'}),
(p)-[:IS_A_PRODUCT_BELONGING_TO_THAT_BRAND]->(b),
(c)<-[:IS_A_PRODUCT_BELONGING_TO_THAT_CATALOG]-(p2:Product {name: 'Amoxicilline',productName:'Amoxicilline',brand:'ANTIINFECTIEUX',brandIdentifier:'ANTIINFECTIEUX',catalog:'ANTIBACTERIENS',catalogIdentifier:'ANTIBACTERIENS',description :'Amoxicilline',DCI:'Amoxicilline', Dosage:'500mg', Forme:'Gélules', Case:'false', Poste:'true', CTRE:'true', CHR:'true', CHN:'true', Reference:'Clamoxyl', price:'250.75', stock:'700',NumeroDeLot:'8777',CodeBarre:'998898979797',QuantiteDemandee:'50'}),
(p2)-[:IS_A_PRODUCT_BELONGING_TO_THAT_BRAND]->(b),
(b1:Brand {name: 'ANESTHESIQUES',brand:'ANESTHESIQUES',description :'ANESTHESIQUES' }),
(b1)<-[:IS_A_CATALOG_BELONGING_TO]-(c1:Catalog {name: 'Anesthesiques_Generaux_et_Gaz_medicaux',catalogName:'Anesthesiques_Generaux_et_Gaz_medicaux',brand:'ANESTHESIQUES',brandIdentifier:'ANESTHESIQUES',description :'Anesthésiques généraux et Gaz médicaux'}),
(c1)<-[:IS_A_PRODUCT_BELONGING_TO_THAT_CATALOG]-(p1:Product {name: 'Kétamine',productName:'Kétamine',brand:'ANESTHESIQUES',brandIdentifier:'ANESTHESIQUES',catalog:'Anesthesiques_Generaux_et_Gaz_medicaux',catalogIdentifier:'Anesthesiques_Generaux_et_Gaz_medicaux',description :'Kétamine',DCI:'Kétamine', Dosage:'50mg/amp', Forme:'Inj', Case:'false', Poste:'false', CTRE:'true', CHR:'true', CHN:'true', Reference:'Kétalar', price:'900.75', stock:'300',NumeroDeLot:'677',CodeBarre:'5454578788',QuantiteDemandee:'10'}),
(p1)-[:IS_A_PRODUCT_BELONGING_TO_THAT_BRAND]->(b1)
满足我需求的请求
MATCH PATH = (Brand:Brand)<-[*]-(Catalog:Catalog)<-[*]-(Product:Product)
with collect(PATH) as paths
call apoc.convert.toTree(paths) yield value
return value
从 neo4j 3.5.0 + 及 apoc.convert.toTree 的签名开始,我们可以包含/排除属性、关系和 as 有 3 个参数
[路径],[lowerCaseRels=true]) | 创建代表这些路径的至少一个根的嵌套文档流
就像在下面
为了排除我们的前缀 -
apoc.convert.toTree(ps, true,{nodes: {Catalog: ['-name']}, 关系: {subcategory:['-id']}})
包括
apoc.convert.toTree(ps, true,{nodes: {Catalog: ['name']}, 关系: {subcategory:['id']}})
但不幸的是,无法从密码结果中删除 _id _type 请参阅该链接[https://stackoverflow.com/questions/56288402/is-there-a-way-to-remove-id-type-from-cypher-result ] 1
推荐阅读
- excel - Excel中的多个嵌套IF语句
- postgresql - Postgres JSONB - 子对象的所有动态(整数)属性的索引
- php - array_key_exists 没有按预期工作
- android - 如何使用单个按钮实现 EditText 和保存编辑的文本?
- wordpress - 如果在 Woocommerce 中购买了特定产品,则向特定地址发送电子邮件通知
- swift - 从一个 IBAction 到另一个 swift 的浮点数
- regex - 正则表达式 unix 命令
- c++ - LuaC 库:某些函数的访问冲突
- createjs - CreateJS 使 Cache Empty 不断占用内存?
- python - 如何在 python 中从 MultiIndexed pandas 数据框创建数组树