Skip to content
This repository has been archived by the owner on Sep 3, 2021. It is now read-only.

How can I reuse "this" as a reference to a current node? #572

Open
dan-fein opened this issue Jan 17, 2021 · 2 comments
Open

How can I reuse "this" as a reference to a current node? #572

dan-fein opened this issue Jan 17, 2021 · 2 comments

Comments

@dan-fein
Copy link

I am using neo4j-graphql-js and in one of my cypher statement's I'm referring to "this" which is the current node. It seems that the word "this" in my first MATCH is accurate, but then when I use MATCH the second time, "this" becomes just a general keyword and defaults to an unlabeled/non-specific node (and just returns everything).

Here's the code currently. it finds any posts that are either tagging "Things" that are children of the current Thing node, or where they're tagging the current Thing node specifically.

    MATCH (this)<-[:CHILD_OF]-(t1:Thing)<-[:TAGGED]-(post:Post)
    WITH COLLECT(post) as rows
    MATCH (this)<-[:TAGGED]-(p:Post)
    WITH rows + COLLECT(p) as allRows
    UNWIND allRows as post
    RETURN count(DISTINCT post)

Any ideas how I can reuse "this" the second time? I tried sharing it after WITH COLLECT(post) as rows, like this:

WITH COLLECT(post) as rows, this

Any ideas would be appreciated, thank you in advance!

@dan-fein
Copy link
Author

dan-fein commented Jan 17, 2021

Is it possibly that "this" doesn't exist, and I need to instead use OPTIONAL, so that if it fails, it still continues on? I think it might be stopping there?

Is that accurate? So this should work?:

OPTIONAL MATCH (this)<-[:CHILD_OF]-(t1:Thing)<-[:TAGGED]-(post:Post)
WITH COLLECT(post) as rows, this
OPTIONAL MATCH (this)<-[:TAGGED]-(p:Post)
WITH rows + COLLECT(p) as allRows
UNWIND allRows as post
RETURN DISTINCT post
ORDER BY post.date DESC

@michaeldgraham
Copy link
Collaborator

#608

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants