Clam
Clam

Reputation: 945

Neo4j gem - Querying relationships that do not exist

I don't really have the code for this yet but I have the opposite of what I need

current_user.friends(:f).match_to(event)

This is going to return the friends that have a relationship to my event node. What I need is the ones that don't have this relationship.

Do I have to feed it something like in the docs

blog.query_as(:blog).match("blog<-[:COMMENTS_ON]-(:Comment)<-[:AUTHORED]-(author:Person)").where("author.age > 30").pluck(:author)

but nil the relationship?

so something like

current_user.friends.query_as(:f).match("event<-[invite:invited]-(f:User)).where(invite: nil)

Or is there another way?

Upvotes: 4

Views: 215

Answers (1)

Brian Underwood
Brian Underwood

Reputation: 10856

That query wouldn't work because if you specify a MATCH in cypher, you're saying that the relationship has to exist. What you need to do is use the match ascii syntax in the WHERE clause (which neo4j begrudgingly supports for exactly this case):

current_user.friends.query_as(:friend).
  match(event: {neo_id: event.neo_id}).
  where("NOT(friend-[:invited]->(event:Event))").
  pluck(:friend)

It occurs to me that you should be able to do match(event: event) on that second line. You might be able to, but I forget.

Also note that the match_to applies to the most recent association in the chain (so in your first example you'd be trying to match the event object with one of the user's friends, which wouldn't work).

Upvotes: 6

Related Questions