databaseneo4jgraph-databasesfraud-prevention

How to query all association (all level) in Neo4j?


I am new to study graph databases (using Neo4j), I am modelling for my project. The business rule is that we need to store the users and devices they used, and this is my current model:

Account A uses device D: (Account A) -[USED]-> (Device D)

Account B also uses device D: (Account B) -[USED]-> (Device D)

=>

(Account A) -[USED]-> (Device D) <-[USED]- (Account B)

In future, account B will use other devices & be related with other accounts.

My questions are:

  1. Is it a good model in this case?

  2. With this model, how to find all associated account with account A?

Thank for you help.


Solution

  • Yes, this model works, as :Account and :Device nodes are distinct in the graph.

    As for figuring out what other accounts that are associated with :Account a (only considering the link via using the same device), simple queries should do the trick. Assuming we have :Account(name) in the graph, and that there's an index on this (for fast lookup of :Account nodes by name), we could use this:

    MATCH (:Account {name:'A'})-[:USED]->()<-[:USED]-(other:Account)
    RETURN DISTINCT other
    

    If :USED relationships always are incoming to :Device nodes, we could simplify this to:

    MATCH (:Account {name:'A'})-[:USED*2]-(other:Account)
    RETURN DISTINCT other
    

    If we also needed the devices used in common between the two connected account nodes, we could include that as a collection of the nodes between the two linked accounts:

    MATCH (:Account {name:'A'})-[:USED]->(device)<-[:USED]-(other:Account)
    RETURN other, collect(device) as sharedDevices