1

I want to find all Foos that are not yet associated with my Bar. I use neo4j.rb (4.1.2) and Rails (4.2). The code I use now, that produces the right output but feels unoptimal is:

@foos = Foo.all.find_all do |foo|
  foo.bars.rels_to(current_bar).count == 0
end 

Is there a better way of doing this with Cypher?

Ed Ytterbrink
  • 376
  • 2
  • 10

1 Answers1

6

Here is one way to do this in Cypher. I assume you are only interested in direct relationships, and that Bar nodes are identified by an id property.

MATCH (b:Bar), (f:Foo)
WHERE b.id = 123 AND NOT (b)--(f)
RETURN b, COLLECT(f);
cybersam
  • 63,203
  • 6
  • 53
  • 76
  • 4
    In Neo4j.rb, that'd be `current_bar.query_as(:b).match('f:Foo').where('NOT (b)--(f)').pluck(:b, 'collect(f)')` – subvertallchris Feb 04 '15 at 22:52
  • 1
    Thank you @cybersam! I had to do change the neo4j.rb version to `current_bar.query_as(:b).match('(f:Foo)').where('NOT (b)--(f)').pluck( 'collect(f)').first` to make it to work (missing ( and ) around f:Foo) and act like my old ruby code. For anyone who reads this. Thank you @subvertallchris! – Ed Ytterbrink Feb 06 '15 at 21:31