10
votes

I have a requirement in which a property key can contain multiple values. How do I store them as property in Neo4j?

Ex: Person node has properties like: 'name', 'age' and 'interests' The 'interests' property can can contain more than one strings(array of strings).

What is the best approach to store 'interests'? I think I don't want to complicate this by adding more nodes. Instead I want to keep all properties in the same Person node.

Also, it will be good if I can search a Person node by any one item in the 'interests' property.

Store as one string separated by some special chars? Store as array of strings for a property? if so how do I do this?

Thanks

1
Have you considered reading the manual? Here is the section on cypher collections - neo4j.com/docs/stable/syntax-collections.htmlDave Bennett
Thanks, looks like I can json encode list of values and store as one value of a property. Is it possible to search(or match) by one of the value in the property?Krishna Shetty
You could certainly store json as a string but then you really would not be able to exploit the database when searching. Like @Brian says, if you are going to search on the 'interests' you are likely best served by making them first class citizens in your model.Dave Bennett

1 Answers

5
votes

You can store an array of strings as a property and that's what I might suggest if you want to simply see a list of interests when working with a particular Person node:

http://neo4j.com/docs/stable/rest-api-property-values.html#_arrays

If you want to look up people by interests, however, I would strongly suggest thinking about storing them as nodes. With the MERGE cypher command it can be quite easy to manage them, and it should be more performant.