Neo4j:如何返回深层节点数据

问题描述 投票:1回答:2

数据集

CREATE
  (u1:User {number: 1}), (u2:User {number: 2}),
  (r1:Room {name: 'r1'}), (r2:Room {name: 'r2'}),
  (d1:UnavailableDate {date: '1/1/2016'}), (d2:UnavailableDate {date: '1/2/2016'}),
  (i1:Image {url: 'http://..'}), (i2:Image {url: 'http://..'}),(i3:Image {url: 'http://..'}),
  (pA:Place {name: 'P'}),
  (u1)<-[:house_mate]-(pA)-[:owner_of]->(u2),
  (pA)<-[:place]-(r1),
  (pA)<-[:place]-(r2),
  (r1)<-[:room]-(d1),
  (r1)<-[:room]-(d2),
  (r2)<-[:room]-(i1),
  (r2)<-[:room]-(i2),
  (r1)<-[:room]-(i3)

以下是我的查询

MATCH (place:`Place` {name: 'P'}),
      (place)-[:place]-(room:Room)
OPTIONAL MATCH (place)-[tenant:owner_of|house_mate]-(u:User)
OPTIONAL MATCH (room)-[:room]-(date:UnavailableDate)
OPTIONAL MATCH (room)-[:room]-(image:Image)
WITH DISTINCT place,
     collect(room) AS r,
     collect(image) AS images,
     collect(date) AS dates,
     type(tenant) AS type,
     u
WITH place,
     collect({type: type, u: u}) AS tenants,
     collect({rooms: r, images: images, dates: dates}) AS rooms
RETURN DISTINCT place,
       rooms,
       [tenant IN tenants WHERE tenant.type = 'owner_of'   | [tenant.u]][0] AS owner_array,
       [tenant IN tenants WHERE tenant.type = 'house_mate' | [tenant.u]] AS house_mates_array

结果enter image description here在这里,我想弄清楚

  1. 地方应该是明显的结果
  2. 每个房间都应该将其不可用的日期和图像作为房间结果中的单独阵列
  3. 所有者和室友阵列看起来不错应该是这样的

问题是收集图像和日期应该在房间不到位

有帮助吗?

neo4j cypher
2个回答
3
投票

重复项来自背对背的可选竞赛。您将获得udateimage的所有组合的交叉产品。为避免这种情况,您需要在每个之后立即收集。或者,更好的是,使用pattern comprehension立即将结果收集到一个集合中,并在最终集合中使用map projection将日期和图像添加到每个相关的房间:

MATCH (place:`Place` {name: 'P'})
WITH place, [(place)-[:owner_of]-(u:User) | u] as owner_array, [(place)-[:house_mate]-(u:User) | u] as house_mates_array
MATCH (p)-[:place]-(room:Room)
WITH place, owner_array, house_mates_array, room, [(room)-[:room]-(date:UnavailableDate) | date] as dates, [(room)-[:room]-(image:Image) | image] as images
RETURN place, collect(room {.*, dates, images}) as rooms, owner_array, house_mates_array

1
投票

这可能接近你想要的:

MATCH (place:`Place` {name: 'P'})-[:place]-(room:Room)
OPTIONAL MATCH (place)-[tenant:owner_of|house_mate]-(u:User)
WITH place, room, TYPE(tenant) AS type, u
OPTIONAL MATCH (room)-[:room]-(date:UnavailableDate)
OPTIONAL MATCH (room)-[:room]-(image:Image)
WITH place, room,
     collect(image) AS images,
     collect(date) AS dates,
     collect(DISTINCT {type: type, u: u}) AS tenants
RETURN place,
       collect({room: room, images: images, dates: dates}) AS room_data,
       [tenant IN tenants WHERE tenant.type = 'owner_of'   | [tenant.u]][0] AS owner_array,
       [tenant IN tenants WHERE tenant.type = 'house_mate' | [tenant.u]] AS house_mates_array;

结果如下:

╒════════════╤══════════════════════════════════════════════════════════════════════╤══════════════╤═══════════════════╕
│"place""room_data""owner_array""house_mates_array"│
╞════════════╪══════════════════════════════════════════════════════════════════════╪══════════════╪═══════════════════╡
│{"name":"P"}│[{"room":{"name":"r2"},"images":[{"url":"http://.."},{"url":"http://..│[{"number":2}]│[[{"number":1}]]   │
│            │"},{"url":"http://.."},{"url":"http://.."}],"dates":[]},{"room":{"name│              │                   │
│            │":"r1"},"images":[{"url":"http://.."},{"url":"http://.."},{"url":"http│              │                   │
│            │://.."},{"url":"http://.."}],"dates":[{"date":"1/1/2016"},{"date":"1/2│              │                   │
│            │/2016"},{"date":"1/1/2016"},{"date":"1/2/2016"}]}]                    │              │                   │
└────────────┴──────────────────────────────────────────────────────────────────────┴──────────────┴───────────────────┘
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.