Neo4j only supports lists that contain primitive types such as string
, int
, bool
, etc (mixed types in a list aren't allowed).
Here are the property types Neo4j supports:
http://docs.neo4j.org/chunked/preview/graphdb-neo4j-properties.html
To store a mixed-type list
in Neo4j, you can save it as a JSON document string.
Bulbs has a Document
Property
type that does the dict
<->json
conversion for you.
See...
- https://github.com/espeed/bulbs/blob/master/bulbs/property.py#L453
- https://github.com/espeed/bulbs/blob/master/bulbs/json.py#L154
- https://github.com/espeed/bulbs/blob/master/bulbs/json.py#L264
If you working with a generic Vertex
or Edge
, you need to do this conversion manually before you save it:
specs = [(2, 0.27911702036756064), (5, 0.6708785014712791)]
anode = g.vertices.get(123)
anode.specs = g.client.type_system.database.to_document(specs)
anode.save()
However, if you are using a Model
, Bulbs will do the conversion for you. Simply define your model using the Document
property instead of List
:
# people.py
from bulbs.model import Node, Relationship
from bulbs.property import String, DateTime, Document
from bulbs.utils import current_datetime
class Person(Node):
element_type = "person"
name = String(nullable=False)
specs = Document()
class Knows(Relationship):
label = "knows"
timestamp = DateTime(default=current_datetime, nullable=False)
...you can then use your model like this...
>>> from people import Person, Knows
>>> from bulbs.neo4jserver import Graph
>>> g = Graph()
>>> g.add_proxy("people", Person)
>>> g.add_proxy("knows", Knows)
>>> specs = [(2, 0.27911702036756064), (5, 0.6708785014712791)]
# You can save specs when you create it...
>>> james = g.people.create(name="James", specs=specs)
# ...or save it after creation...
>>> julie = g.people.create(name="Julie")
>>> julie.specs = specs
>>> julie.save()