cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Return certain properties from apoc.convert.toTree

melgamal
Node Link

I have a graph of Restaurants that are located in different plazas

I'm using the following statement but it's returning all the properties from Plaza and Restaurant

MATCH r=(p:Plaza)-[:in_plaza]->(rs:Restaurant)
WITH COLLECT(r) AS rs
CALL apoc.convert.toTree(rs) yield value
RETURN value

How I can only return certain properties instead of the whole object property same as when i do
MATCH (p:Plaza)-[:in_plaza]->(rs:Restaurant) return p.name as plaza, rs.name as restaurant only returns these two properties

1 ACCEPTED SOLUTION

jsmccrumb
Graph Buddy

Ah ok, its possible that this will do it:

nodes: map of list, a key is the name of the label, value is a list of properties that can be included or excluded. e.g nodes:{LABEL:<[prop1, prop2]/[-prop1, -prop2]>}

github changeset

So:

MATCH r=(p:Plaza)-[:in_plaza]->(rs:Restaurant)
WITH COLLECT(r) AS rs
CALL apoc.convert.toTree(rs, {
    nodes: {Plaza: ['name'], Restaurant: ['name']}
}) yield value
RETURN value

might do it no promises!

View solution in original post

6 REPLIES 6

jsmccrumb
Graph Buddy

Do you know what version of APOC you are using? I couldn't find the documentation for apoc.convert.toTree

melgamal
Node Link

i'm using version 3.5.0.3, it's true i can't find the documentation either, but there are multiple discussions on stack overflow with examples using it. it's documented in 3.3 https://neo4j-contrib.github.io/neo4j-apoc-procedures/index33.html#_from_tojson
below is a snapshot of the results i'm getting.

I'm thinking that if there's a way to filter the properties before using this function

jsmccrumb
Graph Buddy

Ah ok, its possible that this will do it:

nodes: map of list, a key is the name of the label, value is a list of properties that can be included or excluded. e.g nodes:{LABEL:<[prop1, prop2]/[-prop1, -prop2]>}

github changeset

So:

MATCH r=(p:Plaza)-[:in_plaza]->(rs:Restaurant)
WITH COLLECT(r) AS rs
CALL apoc.convert.toTree(rs, {
    nodes: {Plaza: ['name'], Restaurant: ['name']}
}) yield value
RETURN value

might do it no promises!

Thanks, It worked, however, i had to pass the second a required second parameter as boolean (lowercase/uppercase relationship names)

MATCH r=(p:Plaza)-[:in_plaza]->(rs:Restaurant) WITH COLLECT(r) AS rs CALL apoc.convert.toTree(rs, true, { nodes: {Plaza: ['name'], Restaurant: ['name']} }) yield value RETURN value

Hello @melgamal, I've fallen into the same need and applied your query that worked for me, thanks by the way for sharing. However in the response I still get the _type and _id params. Did you manage to remove it as well from the response. Thanks

so far no, That's something i'm looking for too. P.S. Thanks to @jsmccrumb for the solution, not to me