Transforming Multi-value Properties

What is a multi-value property?

A multi-value property is a property that can hold one or more values. This type of data in Neo4j is represented as a list. All values in a list must have the same type. For example:

  • ["USA", "Germany", "France"]

  • [100, 55, 4]

Multi-value properties in our data model

Our current data model defines lists for these properties:

  • Movie: countries

  • Movie: languages

Because of the way that we imported the data, we have an additional multi-value property, Movie: genres. Although our data model does not contain a genres property for Movie nodes, we will transform the data for this property as it will be a means to our goal of creating the Genre nodes in the graph in a later transformation.

All of these properties were imported as strings with data that looks as follows:

Multi-value properties

Notice that for this particular set of CSV files, the "|" character is specified as the separator.

Transforming list properties

Transforming multi-value fields as lists can be done as follows where we use two Cypher built-in functions to help us:

You will execute this code in the next Challenge.

Cypher
Unresolved directive in lesson.adoc - include::{repository-raw}/main/modules/3-refactoring-imported-data/lessons/3-transform-lists/transform-lists.cypher[]

coalesce() returns an empty string if the entry in m.countries is null. split() identifies each element in the multi-value field where the "|" character is the separator and create a list of each element.

So the resulting transformations will look like this:

Movie list properties transformed

Checking the property types

Again, you can confirm that the property types for the three Movie properties has been completed also by viewing the types again:

StringArray for lists transformed

The three list properties have been transformed to the type StringArray.

Check your understanding

1. Storage type for lists of strings

When you transform a multi-value property to a list of strings, what type does it have in the graph?

  • ❏ List

  • ❏ Strings

  • ❏ Array

  • ✓ StringArray

Hint

You can execute CALL apoc.meta.nodeTypeProperties() to view how properties are stored in the graph.

Solution

A list of strings is stored as StringArray in the graph.

2. Transform to a list

Suppose you want to transform the data in a field to be a list of strings. What built-in Cypher functions help you to do this?

  • toString()

  • toList()

  • coalesce()

  • split()

Hint

You use two functions, one to identify the elements separated by a character and one to create the list.

Solution

The correct answers are: coalesce() and split().

Summary

In this lesson, you learned how to transform multi-value properties to lists. In the next Challenge, you will transform multi-value properties in the graph you have been working with to list properties.