首页 > 解决方案 > Display two separated records according to multiple values in properties neo4j

问题描述

I faced a need to make a strange thing. I have some query which is can’t be changed. It’s a match query for getting record:

MATCH (j:journal) WHERE j.id in [12] RETURN j.`id` AS ID, j.`language` AS LANGUAGE

And I have some node that contains array as property: e.g. can be created like this: create (j:journal {id:12, language:[“English”, “Polish”]})

So, is there any possibility to display this node like two records with the same id, but with different language fields? Like the following:

ID | LANGUAGE

12 | English

12 | Polish

The important thing is that match query can’t be changed at all. But the node can be changed.

I know that I can add UNWIND keyword for the language field in the source query. But there is a requirement to not to.

I didn’t find something like that in the documentation nor in the internet. I’m not sure if it’s even possible (but consumer wants it). Just I don’t have much experience with neo4j.

I understand that it can sound weird, but I need to understand if it can be implemented this way.

Thanks in advance.

标签: neo4j

解决方案


If you can change the DB, you can change it so that each journal node contains a single language (as a scalar value, not in a list). However, this change might break any other queries that you might have.

If this conversion is acceptable, here is a query that should: (a) convert existing journal nodes to have a scalar language value, and (b) create new journal nodes as necessary for the remaining language values. The nodes that are spawned from an original journal node will share the same properties (except for language).

MATCH (j:journal)
WITH j, j.language[1..] AS langs
SET j.language = j.language[0]
WITH j, langs
UNWIND langs AS lang
CREATE (k:journal)
SET k = j, k.language = lang

If a node's language property had N values, you will end up with N nodes, each with the same properties -- except for the language property, which will contain a different language value (as a string). For efficiency, the original node is reused.


推荐阅读