【问题标题】:how to use Neo4J driver to query nodes and related nodes and adding properties to the returned nodes如何使用 Neo4J 驱动程序查询节点和相关节点并为返回的节点添加属性
【发布时间】:2020-01-08 20:37:02
【问题描述】:

我正在尝试从 Neo4J 数据库中检索节点和相关节点,我还想在其中动态地将属性添加到返回的节点。我有以下模型:一个国家包含城市,我有 以下示例数据:

CREATE (n:Country { identifier: '20f95f76-be40-4cff-98c8-8d045b0552eb', revision: '1', countryCode: 'BE', name: 'Belgium', type:'Country' });
CREATE (n:Country { identifier: '4c78a5b2-7fe6-4b8c-bd35-f2a0aa0ec160', revision: '1', countryCode: 'NL', name: 'Netherlands', type:'Country' });
CREATE (n:Country { identifier: 'aa265519-4765-477d-99f9-c65bf0c202d8', revision: '1', countryCode: 'DE', name: 'Germany', type:'Country' });

CREATE (n:City { identifier: '8147f29c-39f2-4123-9b73-298de2b871f9', revision: '1', name: 'Antwerp', type:'City' })
CREATE (n:City { identifier: '8058bfc4-9fd3-4df7-8035-0adb53da713c', revision: '1', name: 'Brussels', type:'City' })

MATCH (country:Country), (city:City)
WHERE country.identifier = '20f95f76-be40-4cff-98c8-8d045b0552eb' AND city.identifier = '8147f29c-39f2-4123-9b73-298de2b871f9'
CREATE (country)-[:CONTAINS]->(city)

MATCH (country:Country), (city:City)
WHERE country.identifier = '20f95f76-be40-4cff-98c8-8d045b0552eb' AND city.identifier = '8058bfc4-9fd3-4df7-8035-0adb53da713c'
CREATE (country)-[:CONTAINS]->(city)

我想从数据库中得到的是 Country 节点和 City 节点,但是我需要向返回的 Country 节点添加一个“city”属性,该属性包含一个标识符的数组包含的城市。而不是存储 _type 属性,我想根据节点的标签动态添加它。

目前我正在使用以下查询:

MATCH (country:Country)
OPTIONAL MATCH (country)-[:CONTAINS]-(city:City)
RETURN country {.*, city: collect( distinct city.identifier )}, city {.*, container: country.identifier}

我得到以下结果:

[
  {
    "country": {
      "city": [
        "8058bfc4-9fd3-4df7-8035-0adb53da713c"
      ],
      "name": "Belgium",
      "identifier": "20f95f76-be40-4cff-98c8-8d045b0552eb",
      "revision": "1",
      "countryCode": "BE",
      "type": "Country"
    },
    "city": {
      "container": "20f95f76-be40-4cff-98c8-8d045b0552eb",
      "identifier": "8058bfc4-9fd3-4df7-8035-0adb53da713c",
      "name": "Brussels",
      "revision": "1",
      "type": "City"
    }
  },
  {
    "country": {
      "city": [
        "8147f29c-39f2-4123-9b73-298de2b871f9"
      ],
      "name": "Belgium",
      "identifier": "20f95f76-be40-4cff-98c8-8d045b0552eb",
      "revision": "1",
      "countryCode": "BE",
      "type": "Country"
    },
    "city": {
      "container": "20f95f76-be40-4cff-98c8-8d045b0552eb",
      "identifier": "8147f29c-39f2-4123-9b73-298de2b871f9",
      "name": "Antwerp",
      "revision": "1",
      "type": "City"
    }
  },
  {
    "country": {
      "city": [],
      "name": "Netherlands",
      "identifier": "4c78a5b2-7fe6-4b8c-bd35-f2a0aa0ec160",
      "revision": "1",
      "countryCode": "NL",
      "type": "Country"
    },
    "city": null
  },
  {
    "country": {
      "city": [],
      "name": "Germany",
      "identifier": "aa265519-4765-477d-99f9-c65bf0c202d8",
      "revision": "1",
      "countryCode": "DE",
      "type": "Country"
    },
    "city": null
  }
]

如您所见,Belgium 节点被返回两次(我原以为它只返回一次),每次在包含的 City 上都有匹配项。我确实想返回 Country 节点,即使它们不包含城市。

我们将不胜感激

【问题讨论】:

    标签: neo4j cypher


    【解决方案1】:

    这个查询可能适合:

    MATCH (country:Country)
    OPTIONAL MATCH (country)-[:CONTAINS]-(city:City)
    RETURN country, COLLECT(city) AS cities
    

    结果是:

    ╒══════════════════════════════════════════════════════════════════════╤══════════════════════════════════════════════════════════════════════╕
    │"country"                                                             │"cities"                                                              │
    ╞══════════════════════════════════════════════════════════════════════╪══════════════════════════════════════════════════════════════════════╡
    │{"name":"Belgium","identifier":"20f95f76-be40-4cff-98c8-8d045b0552eb",│[{"name":"Antwerp","identifier":"8147f29c-39f2-4123-9b73-298de2b871f9"│
    │"type":"Country","countryCode":"BE","revision":"1"}                   │,"type":"City","revision":"1"},{"name":"Brussels","identifier":"8058bf│
    │                                                                      │c4-9fd3-4df7-8035-0adb53da713c","type":"City","revision":"1"}]        │
    ├──────────────────────────────────────────────────────────────────────┼──────────────────────────────────────────────────────────────────────┤
    │{"name":"Netherlands","identifier":"4c78a5b2-7fe6-4b8c-bd35-f2a0aa0ec1│[]                                                                    │
    │60","type":"Country","countryCode":"NL","revision":"1"}               │                                                                      │
    ├──────────────────────────────────────────────────────────────────────┼──────────────────────────────────────────────────────────────────────┤
    │{"name":"Germany","identifier":"aa265519-4765-477d-99f9-c65bf0c202d8",│[]                                                                    │
    │"type":"Country","countryCode":"DE","revision":"1"}                   │                                                                      │
    └──────────────────────────────────────────────────────────────────────┴──────────────────────────────────────────────────────────────────────┘
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-14
      • 2023-01-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多