neomodel是neo4j数据库的OGM(Object Graph Mapper)的python库,基于neo4j_driver开发。当在开发过程中需要查询节点的属性时,用这个库就会非常方便,也使代码简洁流畅。
安装neonodel的Python包
pypi源安装
从github安装
连接neo4j数据库
config配置
db配置
Nodes
通过StructuredNode构建映射节点类
Country和Person节点类都是继承StructuredNode构建的,节点类的属性通过实例化不同的数据类构建,这些属性也只是我们自己创建的节点属性,与neo4j自己生成的节点属性无关,比如id,labels等。
Create, Save, Delete操作
实例化节点、属性赋值、保存、删除、重载、等
查找节点
使用“.nodes”类属性查找节点,根据条件筛选节点
Relationships
使用StructuredRel构建映射边的类
通过Relationship构建两个类之间无向的关系,通过RelationshipTo或RelationshipFrom构建类之间有向关系,通过model指定关系实例继承的类,继承相关的属性。
基数限制
如果对边加上基数限制,需要在边的两个方向上分别去做定义,对cardinality作设置。
实例化及常用方法
通过connect方法实例化一条边,还可以指定属性筛选;
通过start_node、end_node可以查看边的属性;
还可以对边进行赋值,并保存。
此外,当两个节点存在一种关系时,则可以使用relationship这种方法
Property types
属性默认值设置,可以是具体值,也可以是函数
StringProperty 的Choices
Array Properties
UniqueIdProperty
Dates and times
DateTimeProperty 对应 UTC epoch value
DateProperty 对应‘YYYY-MM-DD’
-Aliasing properties
依赖属性支持节点属性设置
独特属性名
使用db_property去创建属性,类似于创建类属性
Other properties
EmailProperty、RegexProperty、NormalProperty
Advanced queries
节点集合与过滤
通过nodes属性获得节点集合,通过filter过滤节点
过滤的方法有以下:
lt - less than
gt - greater than
lte - less than or equal to
gte - greater than or equal to
ne - not equal
in - item in list
isnull - True IS NULL, False IS NOT NULL
exact - string equals
iexact - string equals, case insensitive
contains - contains string value
icontains - contains string value, case insensitive
startswith - starts with string value
istartswith - starts with string value, case insensitive
endswith - ends with string value
iendswith - ends with string value, case insensitive
regex - matches a regex expression
iregex - matches a regex expression, case insensitive
查看具有关系
利用has方法查看节点是否有一种或多种方法,True 为有,False为无
迭代性、切片、计数等
过滤关系
使用match方法过滤关系
通过属性排序
使用order_by方法排序,加’-‘倒排
指定None则不排序,使用’?’随机排序
7. Cypher queries
通过StructuredNode的inflate类方法实现cypher查询
StructuredNode外构建cypher
参考链接:https://neomodel.readthedocs.io/en/latest/index.html
其他相关文章:
领取专属 10元无门槛券
私享最新 技术干货