首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >生成嵌套递归json的Cypher请求

生成嵌套递归json的Cypher请求
EN

Stack Overflow用户
提问于 2019-10-05 03:49:51
回答 1查看 60关注 0票数 0

到目前为止,我已经创建了以下节点:

代码语言:javascript
运行
复制
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,如下所示:

代码语言:javascript
运行
复制
{ 
   "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文件,我发出了以下cypher请求:

代码语言:javascript
运行
复制
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 )不匹配,正如您在这里看到的那样。

代码语言:javascript
运行
复制
"{
"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"]
}"

如果有人能帮助我实现这个目的,那就太好了。

代码语言:javascript
运行
复制
{ 
   "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"
                          .....................
                        }
                   ]
               },
    ]

}

]
}

非常感谢。

EN

Stack Overflow用户

发布于 2019-10-06 03:50:23

代码语言:javascript
运行
复制
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)    

满足我需求的请求

代码语言:javascript
运行
复制
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,{节点:{目录:‘-名称’},关系:{子类别:‘-id’}})

包含以下内容

apoc.convert.toTree(ps,true,{节点:{目录:‘名称’},关系:{子类别:‘id’}})

但不幸的是,无法从cypher结果中删除_id _type请参阅链接[https://stackoverflow.com/questions/56288402/is-there-a-way-to-remove-id-type-from-cypher-result]1

票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58242428

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档