2014-04-21 10 views
5

Sto avendo 2 tipi di nodi diciamo di tipo 'Student' e 'Maestro'Come trovare nodo con alcuna relazione in arrivo in Neo4j

Student have {id, name}. 
Teacher have {id, name}. 

studente può avere rapporti opzionale con il nodo di classe come 'INSEGNA'.

(t:Teacher)-[r:TEACHES]->(c:Student). 

[r:TEACHES] - Optional relationship. (present or may not present) 

Voglio trovare nodi "Studenti" che non hanno insegnante. Non ci sono relazioni in entrata "INSEGNAMENTI"

Please help.

risposta

4

Ecco una semplice impostazione dei dati, insieme alla query in basso è necessario per risolvere il problema. In sostanza, si desidera eseguire una query per le situazioni in cui una relazione non esiste. La sintassi qui è per neo4j 2.0, quindi la risposta sarebbe leggermente diversa per le versioni precedenti.

neo4j-sh (?)$ create (t:Teacher {name:"Bob"})-[r:TEACHES]->(s:Student {name:"Mary"}); 
+-------------------+ 
| No data returned. | 
+-------------------+ 
Nodes created: 2 
Relationships created: 1 
Properties set: 2 
Labels added: 2 
19 ms 

neo4j-sh (?)$ create (t:Teacher {name:"Mark"}); 
+-------------------+ 
| No data returned. | 
+-------------------+ 
Nodes created: 1 
Properties set: 1 
Labels added: 1 
5 ms 

neo4j-sh (?)$ MATCH (s:Student) WHERE NOT (s)<-[:TEACHES]-(:Teacher) RETURN s 
+0

Volevo trovare nodi "Studenti" che non hanno insegnante. Grazie per l'aiuto. –

+0

Basta eseguire la query nella direzione opposta: match (s: Student) dove not (s) <- [: TEACHES] -() return s; – FrobberOfBits

1

Ho ottenuto risultato da questo. La prima corrispondenza con i criteri degli studenti e la relazione di ricerca sono nulli

MATCH (s:Student) 
OPTIONAL MATCH (t:Teacher)-[r:TEACHES]->(s) 
WITH s,r 
WHERE r IS NULL 
RETURN s.name 
+0

Puoi profilare questa query e confrontarla con Wes ', penso che il suo sia probabilmente più semplice (e lascerei cadere l'etichetta ': Teacher' a meno che tu non abbia nodi non-teacher che anche' - [: TEACH] -> 'studente -nodes). – jjaderberg

9

Penso che stiate cercando questo tipo di schema.

MATCH (s:Student) 
WHERE NOT (s)<-[:TEACHES]-(:Teacher) 
RETURN s 
Problemi correlati