Can neo4j store AND and OR operators?

Please keep the following things in mind:

  1. I was trying to create a subject node, the format of the node will be
    title: "Python",
    cp: 6,
    prerequisite: "cs23" OR "Ics12"
    prohibited: "cs207" OR "asd207" OR cs119"
    How can I create attributes for prerequisite and prohibited(it can store OR and AND ) operators?
  2. This is the query I tried:
    CREATE (n:Subject {title: 'Python', cp: '6' ,prerequisite:["cs23",or,"Ics12"], prohibited: ["cs207",OR "asd207",OR cs119"]})

Hi,
Neo4j does not support logical expressions (like 'cs23' OR 'Ics12'), as per the documentation.
Here, you have several options to define "prerequisite" and "prohibited":

1 . First option: create attributes prohibited and prerequisites as a list of strings (assumming you will have only one logical operator applied to all strings). The CREATE code will be the following

CREATE (n:Subject {title: 'Pythonlala', cp: '6' ,prerequisite:['cs23','Ics12'], prohibited: ['cs207','asd207','cs119']})

2 . Second option: In this case, you can take advantage of graph structure and convert prerequisite and prohibited attributes into relationships between two Subjects (or a Subject and other entity). So, you can convert prerequisites and prohibited strings into new Subject nodes, and then connect them to the Python node using relationships, as done on the code below:

CREATE (n:Subject {title: 'Python', cp: '6'}),
(pr1:Subject {title: 'cs23'}),
(pr2:Subject {title: 'Ics12'}),
(pr3:Subject {title: 'cs207'}),
(pr4:Subject {title: 'asd207'}),
(pr5:Subject {title: 'cs119'});

MATCH (n:Subject {title: 'Python'}), (n2:Subject)
WHERE n2.title IN ['cs23', 'Ics12']
CREATE (n)-[:PREREQUISITE]->(n2);
MATCH (n:Subject {title: 'Python'}), (n2:Subject)
WHERE n2.title IN ['cs207','asd207', 'cs119']
CREATE (n)-[:PROHIBITED]->(n2);

In this case, you can also add some relationship attributes (for example, indicating if the relationship involves an "OR" or "AND"). And also you can reuse same entities without duplicating information (a Subject can be a prerequisite for other subject) and do queries in an easier way. I'd rather go with second option

Hope it helps

Thanks for answering the question, what if the prerequisites become more complex? something combined with OR and AND. ("cs23" or "cs37") AND("cs38" or "cs40)?

Hi,

Then, in that case you should follow the second option I gave on my first reply, related to build a new Relationship, and you can add attributes to set up the "AND" or "OR". Like, for example:

MATCH (n:Subject {title: 'Python'}), (n2:Subject)
WHERE n2.title IN ['cs23', 'Ics12']
CREATE (n)-[r:PREREQUISITE ]->(n2)
SET r.operator = 'OR'

Also, you can tune your entities and relationships according to your requirements.

Hope this helps

Thank you so much for helping me to solve the issues!