neo4j cypher-如何查找与节点列表有关系的所有节点 [英] neo4j cypher - how to find all nodes that have a relationship to list of nodes

查看:6461
本文介绍了neo4j cypher-如何查找与节点列表有关系的所有节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为选项"的节点. 用户"选择这些选项.我需要一个像这样的chpher查询:

I have nodes- named "options". "Users" choose these options. I need a chpher query that works like this:

检索选择了所有选项的用户,这些用户将以列表的形式给出.

retrieve users who had chosen all the options those are given as a list.

MATCH (option:Option)<-[:CHOSE]-(user:User) WHERE  option.Key IN ['1','2','2'] Return user

此查询为我提供了选择option(1),option(2)和option(3)的用户,还为我提供了仅选择option(2)的用户.

This query gives me users who chose option(1), option(2) and option(3) and also gives me the user who only chose option(2).

我只需要选择了所有选项的用户-option(1),option(2)和option(3).

What I need is only the users who chose all of them -option(1), option(2) and option(3).

推荐答案

对于全密码解决方案(不知道它是否比Chris的答案更好,您必须进行测试和比较),您可以收集为每个用户过滤掉列表中每个值都没有option.Key的用户

For an all cypher solution (don't know if it's better than Chris' answer, you'll have to test and compare) you can collect the option.Key for each user and filter out those who don't have a option.Key for each value in your list

MATCH (u:User)-[:CHOSE]->(opt:Option)
WITH u, collect(opt.Key) as optKeys
WHERE ALL (v IN {values} WHERE v IN optKeys)
RETURN u

或匹配其键在列表中的所有选项以及选择它们的用户,按用户收集这些选项,并将选项集合的大小与列表的大小进行比较(如果您在列表中没有重复的话)列出具有相同大小的选项集合的用户已选择所有选项)

or match all the options whose keys are in your list and the users that chose them, collect those options per user and compare the size of the option collection to the size of your list (if you don't give duplicates in your list the user with an option collection of equal size has chosen all the options)

MATCH (u:User)-[:CHOSE]->(opt:Option)
WHERE opt.Key IN {values}
WITH u, collect(opt) as opts
WHERE length(opts) = length({values}) // assuming {values} don't have duplicates
RETURN u

这两种方法都应将结果限制为与所有在{values}中指定其键值的选项相关联的用户,并且您可以更改collection参数的长度而无需更改查询.

Either should limit results to users connected with all the options whose key values are specified in {values} and you can vary the length of the collection parameter without changing the query.

这篇关于neo4j cypher-如何查找与节点列表有关系的所有节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆