Search code examples
pythonpython-3.xneo4jneomodel

How to get a node by his id


So I have the id of a node and I want to get the node. For example I have the ID 1 and I want to print out the name of the node with the ID 1.

I mean btw the "internal" ID of neo4j: enter image description here


Solution

  • Depends on how you want to do it. In neomodel, I think StructuredNode class instances have a ._id field. So if you have a node in memory, you can get its ID that way.

    If you don't have a node that way, you can use cypher and the id() function:

    neo4j-sh (?)$ CREATE (a:Foo {label: "Hello"}), (b:Foo {label: "Goodbye"});
    +-------------------+
    | No data returned. |
    +-------------------+
    Nodes created: 2
    Properties set: 2
    Labels added: 2
    1985 ms
    neo4j-sh (?)$ MATCH (f:Foo) return id(f);
    +-------+
    | id(f) |
    +-------+
    | 0     |
    | 1     |
    +-------+
    2 rows
    324 ms
    

    Edit Access by ID:

    MATCH (n) where id(n) = 1 return n;
    
    MATCH (n) where id(n) IN [1,2] return n;