Neo4j 密码 : Group nodes by the relation to another node

标签 neo4j cypher

有一个像 post-->category 这样的图,我怎样才能获得一个类别的帖子?

IE。:

Having
    Post A1 --> Category A
    Post A2 --> Category A
    Post B1 --> Category B
    Post B2 --> Category B
    Post B3 --> Category B
    Post C1 --> Category C

I should get Post A2, Post B1, Post C1. 

我不介意我为给定类别获得什么职位,只是为每个类别获得一个。

谢谢!

最佳答案

要随机选择每个类别的帖子:

MATCH (p:Post)-[:HAS_CATEGORY]->(c:Category)
WITH c, collect(p) as posts
RETURN c, posts[toInt(rand()*length(posts))]

我们正在使用 collect每个类别的聚合函数,在返回时我们随机选择一个条目。

关于Neo4j 密码 : Group nodes by the relation to another node,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29925814/

相关文章:

java - Neo4j 中具有一级关系的路径

node.js - 我如何在 Node.js 项目的 Arango 或 Neo4j 中存储和管理数据库模式

ruby-on-rails - 基于与其他模型的关联自动命名对象

neo4j - 从 Neo4j 数据库中的所有节点中删除属性

neo4j - Neo.TransientError.General.OutOfMemoryError

java - 我应该如何通过java获取neo4j图中的所有现有关系?

neo4j - Cypher支架查询列出所有断开图表的Neo4j图形数据库?

Neo4j,密码 : Conditional Create

neo4j - 在单个查询中比较 Cypher/Neo4J 中的节点计数

neo4j - 有什么方法可以编写存储过程或类似的东西,但在 Cypher 而不是 Java 中?