Neo4j: "does not contain" queries

Ninja

I am trying to find all users whose gender does not contain the string "male". I expect the result of this query to be those users who have not specified a gender.

Users and genders are separate nodes, connected like so:

(user node) - [:gender] -> (gender node)

For some users, the gender relationship does not exist

I ran this query:

MATCH (g:genders) 
WHERE g.gender =~ ".*male.*" 
WITH g MATCH (u:users) 
WHERE NOT (u)-[:gender]->(g) 
RETURN COUNT(u);

The problem with this query is that it returns all the users in my database because the WHERE NOT clause is doing an OR comparison instead of AND ie it is finding all users whose gender is not male or not female, which is all users in my db.

How can I make it an AND query? I would like a solution that does not assume the end node (in this case the gender node) to always have only two possible values.

cybersam

Does this query do what you intend?

MATCH (u:user)
WITH COLLECT(u) AS cu
UNWIND cu AS u
MATCH (u)-[:gender]->(g:gender) WHERE g.gender =~ ".*male.*"
WITH cu, COLLECT(u) AS cv
RETURN REDUCE(s=[], x IN cu | CASE WHEN NOT x IN cv THEN s + x ELSE s END) AS result;

It removes users who have a gender containing "male" from the total set of users.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事