0
votes

I am using Neo4j to implement a social network. The model of the timeline of a user is like the following.

A user can have two activities: Publish and Share, each is created as a node with corresponding labels. All activity nodes are connected through an edge with type NEXT one by one ordered by created time. The model is like:

(:User)-[:TIMELINE]->(:Publish)-[:NEXT]->(:Share)-[:NEXT]->(:Share)->[:NEXT]->(:Publish)->...

Now the problem is, when I want to fetch the nodes, I don't know how to perform queries according to the label of nodes.

For example, when the node is Publish, I have to do something; and when the node is Share, I have to do some other completely different stuff.

Is there a way to achieve this?

2

2 Answers

1
votes

Cypher has the labels function returning an array containing the labels of that node:

MATCH (:User {userID:'123'})-[:TIMELINE]->()-[:NEXT*0..100]->(activity)
RETURN "Publish" in labels(activity) as isPublish, "Share" in labels(activity) as isShare

In case you want to do a mutating operation conditionally, see my reply https://stackoverflow.com/a/26567379/158701.

0
votes

Use either:

return coalesce(node.name, node.author)

or

return case when node:Publish then node.name when node:Share node.author else "unknown" end