Dan
Dan

Reputation: 760

How to return a list of composite objects in Cypher?

If I wanted to return a composite object, based on an exisiting node, I know that I could to this: (thanks to this post)

MATCH(...)-[:HAS_DB]->(db: Database {name: "my_database")

WITH { name: db.name,
       format: db.format,
     } AS database

RETURN database;

This would return an object based on but not exactly my Database node.

However, I would like to return composite objects for a collection of nodes, not just for one node:

I tried this, but it seems like FOREACH is only appropriate for List<T>.

MATCH(...)-[:HAS_DB]->(databases: Database)

FOREACH (db IN databases |
         RETURN {
            name: db.name,
            format: db.format
         }
        )

How could I do this?

Upvotes: 1

Views: 1743

Answers (1)

TheTeacher
TheTeacher

Reputation: 510

MATCH(...)-[:HAS_DB]->(db: Database {name: "my_database")
WITH DISTINCT db
Return COLLECT( { name: db.name,
       format: db.format,
     }) AS database

Upvotes: 2

Related Questions