user2725064
user2725064

Reputation: 11

Best practice to store data in neo4j

I am using neo4jphp for developing web application of forum where multiple users will comment on same topic and also can comment on comments(like nested comments).

So what is the best practice to store this data in neo4j?

I have thought of following options:

  1. Create new node for each comment, sub comment and create relationships between topics and comments and sub comments

  2. Create node for each topic, each comment and create relationship between them and add all comments as properties to same node of comment.

I think 1st option is better but I am worrying about the number of nodes to be created as number of topics and comments will increase.

Does neo4j performance, efficiency deteriorate as number of nodes increases?

Should we create separate node for each record/row in sql?

What is the best practice?

Upvotes: 0

Views: 800

Answers (1)

SotirisTsartsaris
SotirisTsartsaris

Reputation: 638

I made my blog at trartsaris.gr entirely with php and Neo4j 2.0. After making a lot of tests and thinking about the future I decided to enable comments the first way you describe. Meaning that in the end of your schema design your database should look like this

(m:Post "or topic")-[:COMMENT]-(n:Comment)-[:SUB_COMMENT]-(s:SubComment)

So we have a node which is the Post or Topic and this node has connected nodes with Label:Comment and relationship:Comment and those comments can have sub comments with Label:SUB_COMMENTS. You don't have to worry about the number of nodes that will be created and how they will be fetched since everything will be fast enough. You can read a bit on my answer on this question. Let's see why I prefer the first way and I am thinking about future potential use cases. You can have later on something like this: I am a user that I have commented on an a comment of a topic of another user. Based on this action I can search in my database for related Topics by this user and make suggestions for further reading.

MATCH (n:User(ID))-[:WROTE_SUB_COMMENT]-(s:SubComment)-[:SUB_COMMENT]-(c:Comment)-[:COMMENT]-(p:Post)-[:WRITTEN_BY]-(k:User)-[:WRITTEN_BY]-(g:Post) WHERE blablabla RETURN g

In a simplified way we say that we want to find all the posts that have been written by users that the initial user has written sub comments for. It might look a bit of confusing at the moment but as you dive more and more into Neo4j you will see that everything will be easier day by day. I hope that helped a bit.

Upvotes: 1

Related Questions