修改密码查询

时间:2017-08-02 05:28:44

标签: neo4j cypher

我有neo4j数据,如下所示:

enter image description here

此处COUNTEY_PROVINCEcountry之间存在province关系,PROVINCE_CITYprovincecity关系COUNTRY_CITY关系在countrycity之间。当用户使用country name调用api时,我想要使用province返回所有city。为此,我运行了以下查询:

 MATCH path=(cn:Country { name: "Bangladesh" })-[:COUNTRY_PROVINCE]->(pv:Province)-[:PROVINCE_CITY]->(ct:City)
 RETURN { x:  nodes(path)  }

我得到以下结果(国家,省,市)

  { x: nodes(path) }
  { "x": [ { "name": "Bangladesh"}, { "name": "Dhaka" }, { "name": "Dhaka" } ] }
  { "x": [ { "name": "Bangladesh"}, { "name": "Dhaka" }, { "name": "Narayanganj" } ] }
  { "x": [ { "name": "Bangladesh"}, { "name": "Sylhet" }, { "name": "Sylhet" } ] }
  { "x": [ { "name": "Bangladesh"}, { "name": "Khulna" }, { "name": "Khulna" }  ] }
  { "x": [ { "name": "Bangladesh"}, { "name": "Khulna" }, { "name": "Jessore" }  ]  }
  { "x": [ { "name": "Bangladesh"}, { "name": "Chittagong" }, { "name": "Chittagong" }  ]  }
  { "x": [ { "name": "Bangladesh"}, { "name": "Chittagong" }, { "name": "Comilla" }  ]  }

现在我的问题是,如何获得省和联系城市的国家/地区列表,如:

 [
    {
       country: {
           name: "Bangladesh",
           province: [
              {
                  name: "Dhaka",
                  city: [
                      { name: "Dhaka" },
                      { name: "Narayanganj" }
                  ]
              },
              {
                  name: "Sylhet",
                  city: [
                      { name: "Sylhet" }
                  ]
              },
              {
                  name: "Chittagong",
                  city: [
                      { name: "Chittagong" },
                      { name: "Comilla" }
                  ]
              },
              {
                  name: "Khulna",
                  city: [
                      { name: "Khulna" },
                      { name: "Jessore" }
                  ]
              }            
           ]
       }
    }
 ]

2 个答案:

答案 0 :(得分:1)

我在这里模拟你的场景。

加载初始数据集(类似于问题中描述的数据集):

CREATE (c:Country {name:"Country A"})
CREATE (p1:Province {name:"Province A"})
CREATE (p2:Province {name:"Province B"})
CREATE (c1:City {name:"City A"})
CREATE (c2:City {name:"City B"})
CREATE (c3:City {name:"City C"})
CREATE (c4:City {name:"City D"})
CREATE (c)-[:COUNTRY_PROVINCE]->(p1)
CREATE (p1)-[:PROVINCE_CITY]->(c1)
CREATE (p1)-[:PROVINCE_CITY]->(c2)
CREATE (c)-[:COUNTRY_PROVINCE]->(p2)
CREATE (p2)-[:PROVINCE_CITY]->(c3)
CREATE (p2)-[:PROVINCE_CITY]->(c4)

查询:

MATCH (cn:Country { name: "Country A" })-[:COUNTRY_PROVINCE]->(pv:Province)-[:PROVINCE_CITY]->(ct:City)
WITH cn, pv, collect({name : ct.name}) as cities
RETURN {coutry : {name : cn.name, province : collect( distinct { name:pv.name, city :cities }) } }

结果:

{
   "coutry":{
      "name":"Country A",
      "province":[
         {
            "name":"Province A",
            "city":[
               {
                  "name":"City B"
               },
               {
                  "name":"City A"
               }
            ]
         },
         {
            "name":"Province B",
            "city":[
               {
                  "name":"City D"
               },
               {
                  "name":"City C"
               }
            ]
         }
      ]
   }
}

上述查询使用collect()函数和DISTINCT运算符来实现所需的格式。

答案 1 :(得分:0)

好吧,如果你要返回一条路径的节点,这显然就是你要获得的。你有几种不同的选择:

  1. 查看https://neo4j-contrib.github.io/neo4j-apoc-procedures/index32.html#_from_tojson,例如 apoc.convert.toTree 可能会让您非常接近您想要的内容。
  2. 自己动手。从任何客户端应用程序发送查询并自行处理(+格式化)结果(使用Java,Python,C#,...)
  3. ...
  4. 希望这有帮助。

    此致 汤姆