1
votes

I am trying to use the COLLECT function to construct an array of objects that use literal map syntax. However, if there is no return value for the property I use, I would rather return an empty array than have object elements of the array retain null values for their properties. How do I ignore these values and return [ ] instead of this?

[
   {
      property_a: null,
      property_b: null
   }
]

Here is my attempted cypher query:

MATCH (n)-[r]-()
WHERE n.id={_nodeid}
WITH n
OPTIONAL MATCH (n)-[instantiationlist:INSTANTIATES]->(target)
WITH n, COLLECT({
        container : instantiationlist.container,
        target    : target.id
    }) AS instantiationset
RETURN n.id AS id, 
       n.name                 AS name, 
       n.color                AS color,
       n.background           AS background,
       LABELS(n)[0]           AS type,
       instantiationset       AS instantiations;

I am trying to have a value of [] for instantiationset instead of this:

[
    {
      "container": null,
      "target": null
    }
]
1

1 Answers

3
votes

This query should work.

OPTIONAL MATCH (n {id: 123})-[ilist:INSTANTIATES]->(target)
WITH n, COLLECT(
  CASE
    WHEN ilist.container IS NULL THEN NULL
    ELSE { container : ilist.container, target : target.id } END
) AS iset
RETURN n.id AS id, n.name AS name, n.color AS color, n.background AS background, LABELS(n)[0] AS type, iset AS instantiations;