bp123
bp123

Reputation: 3417

Store array of objects within a node - Neo4j

I'm trying to use a node like I would use a mongodb document. I'd like to store information about a person on a Person node. This works fine, however, I get an error when I try and insert an Array of objects.

Neo4j only supports a subset of Cypher types for storage as singleton or array properties. Please refer to section cypher/syntax/values of the manual for more details.

Is there a way to do this or is this a limitation with Neo4j.

return session.run(
  "MERGE (person:Person {name: $name})" + 
  "SET person = {name: $name, tests: $tests}", 
  {
    name: name,
    tests: [
      { name: "Test 1", subject: "Math" },
      { name: "Test 2", subject: "English" }
    ]
  }
);

Upvotes: 0

Views: 1476

Answers (2)

Graphileon
Graphileon

Reputation: 5385

conceptually @cybersam is right. but, if you REALLY need to store objects in a node property, you can always convert it to a JSON. Neo4j's apoc library has a function for that: apoc.convert.toJson()

Upvotes: 0

cybersam
cybersam

Reputation: 66967

With a graph DB, the way you store an "array of N objects" related to a node is via creating relationships from that object to N other nodes.

For example:

return session.run(
  "MERGE (person:Person {name: $name}) " +
  "FOREACH (x IN $tests |
     MERGE (t:Test {name: x.name}) ON CREATE SET t = x
     MERGE (person)-[:HAS_TEST]->(t))", 
  {
    name: name,
    tests: [
      { name: "Test 1", subject: "Math" },
      { name: "Test 2", subject: "English" }
    ]
  }
);

Upvotes: 1

Related Questions