Accessing Graph Types

Accessing Nodes

Select the correct method to get the Node from a record:

Java
var records = result.records();
records.forEach(r -> {
    Node node = r./*select:get("person").asNode()*/;
});
  • ❏ get("person")

  • ❏ asNode("person")

  • ✓ get("person").asNode()

  • ❏ node.get("person")

Hint

The get() method allows you to retrieve a node from a record, and the asNode() method converts it to a Node object.

Solution

The correct answer is get("person").asNode();. This retrieves the Node object from the record and converts it to a Node type.

Java
Node node = record.get("node").asNode();

Accessing Relationship Type

Select the correct property to access the type of relationship.

Java
var records = result.records();
records.forEach(r -> {
    Relationship actedIn = r.get("actedIn").asRelationship();

    var rel_type = actedIn./*select:type()*/;
};
  • ❏ name()

  • ❏ relationship_type()

  • ✓ type()

  • ❏ label()

Hint

The relationship type (e.g., ACTED_IN`) is accessed through a simple method on the relationship object.

Solution

The correct answer is type(). This method returns the type of the relationship as a String.

Java
Relationship actedIn = record.get("actedIn").asRelationship();
var rel_type = actedIn.type();

The type() method is the standard way to access the relationship type in Neo4j’s Java driver.

Lesson Summary

In this lesson, you correctly identified how to access properties from nodes and the type of a relationships.

In the next lesson, you will learn how to work with dates and times.