neomodel使用记录

2,302 阅读1分钟

neomodel官方文档

https://neomodel.readthedocs.io/en/latest/getting_started.html

安装neomodel库

pip install neomodel 

连接neo4j,并配置参数

from neomodel import *

config.DATABASE_URL = 'bolt://neo4j:neo4j@localhost:7687'
config.ENCRYPTED_CONNECTION = False 
config.MAX_POOL_SIZE = 50
config.AUTO_INSTALL_LABELS = False

创建节点model

class Person(StructuredNode):
    uid = UniqueIdProperty()
    name = StringProperty()
    age = IntegerProperty(index=True, default=0)

    country = RelationshipTo(Country, 'IS_FROM')
    
install_labels(Person)

根据model创建数据,并保存到neo4j数据库中

jim = Person(name='Jim123', age=3).save()
jim.age = 4  # 可以修改数据
jim.save()  # 保存数据到neo4j

查找新增的数据,检查是否成功

jim = Person.nodes.get(name='Jim123')
print(jim)

返回结果:

{'uid': '210b91b2e74549cb977b38f3296e617a', 'name': 'Jim123', 'age': 4, 'id': 391}

新增数据成功!