【问题标题】:Neo4j Py2neo not updating node propertiesNeo4j Py2neo 不更新节点属性
【发布时间】:2017-03-11 13:22:38
【问题描述】:

编写python程序来获取现有的Neo4j节点并使用py2neov3 package更新属性。

电影节点具有标题和年份属性。有一个 python 字典,其中包含要添加的电影列表。

我尝试了以下选项,正在添加电影节点。但是年份属性没有更新。

选项#1:使用 Py2neo OGM。开始事务,创建电影对象,填充标题,调用合并,填充年份,调用推送,最后提交

选项#2: 代替 OGM(下面的注释代码),使用 Node 函数,调用合并和推送。

我已经完成了上述选项,但它对我不起作用。 Python 3.5.2 版

代码:

try:  
tx = gdb.begin()   ##gdb is Graph object 
for x in moviedict.keys():
 m1 = Movie()
 m1.title = moviedict[x]['title']
 tx.merge(m1)
 m1.year = moviedict[x]['year']
 tx.graph.push(m1)       
tx.commit()
"""Option2 for x in moviedict.keys():
   m1 = Node('Movie',title=moviedict[x]['title'])
   gdb.merge(m1)
   m1['year'] = moviedict[x]['year']
   gdb.push(m1)
 """

谁能帮我解决这个问题?
非常感谢您的帮助。
最好的问候。

【问题讨论】:

    标签: neo4j py2neo


    【解决方案1】:

    我没有使用字典,因为我希望示例既简短又可运行

        import py2neo
        import py2neo.ogm
    
        from py2neo import Graph
        from py2neo.ogm import GraphObject, Property
    
        class Movie(GraphObject):
            __primarykey__ = "title"
    
            title = Property()
            released = Property()
    
        def authenticateAndConnect():
            py2neo.authenticate('localhost:7474', 'user', 'password')
            return Graph('http://localhost:7474/default.graphdb/data/')     
    
        def foo():
            graph = authenticateAndConnect()
            movie = Movie.select(graph).where("_.title = 'The Matrix Reloaded'").first()
            movie.released = 2017
            graph.push(movie)
    
        if __name__ == '__main__':
            foo()
    

    【讨论】:

    • 谢谢。实际上,如果电影不存在,则尝试使用合并来更新或添加,然后是 push,即添加没有年份的电影。您的代码适用于更新。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多