# How return list of node id from shortest path

**URL:** <https://community.neo4j.com/t/how-return-list-of-node-id-from-shortest-path/42751>\
**Category:** Cypher\
**Tags:** cypher\
**Created:** [August 11, 2021, 11:09am UTC](https://community.neo4j.com/t/how-return-list-of-node-id-from-shortest-path/42751 "2021-08-11T11:09:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jkarippery](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/jkarippery/32/5653_2.png) [@jkarippery](https://community.neo4j.com/u/jkarippery)\
**Post date:** [August 11, 2021, 11:09am UTC](https://community.neo4j.com/t/how-return-list-of-node-id-from-shortest-path/42751/1 "2021-08-11T11:09:02Z")

</div>

hello,  
I would like to return a list of nodeId in the path.

```auto
MATCH (m),(o),
p = shortestPath((m)-[*..15]-(o)) 
WHERE id(m) = 11 AND id(o) = 23
RETURN nodes(p)

```

How to return just id of nodes from this path?

**output I expected**  
`[11,4,9,27,0,20,23]`

---

<div class="post-metadata">

**Author:** ![akollegger1](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/akollegger1/32/14828_2.png) [@akollegger1](https://community.neo4j.com/u/akollegger1)\
**Post date:** [August 11, 2021, 12:54pm UTC](https://community.neo4j.com/t/how-return-list-of-node-id-from-shortest-path/42751/2 "2021-08-11T12:54:00Z")

</div>

You could use a list comprehension, which is like using `map` to transform an array of somethings into a an array of something else.

For example, modifying your example to use the movie graph:

```auto
MATCH (m:Movie {title:"The Matrix"}),(o:Person {name:"Tom Hanks"}),
p = shortestPath((m)-[*..15]-(o)) 
RETURN [x in nodes(p) | id(x)]

```

Best,  
ABK

Reference: [Lists - Cypher Manual](https://neo4j.com/docs/cypher-manual/current/syntax/lists/#cypher-list-comprehension)

---

<div class="post-metadata">

**Author:** ![jkarippery](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/jkarippery/32/5653_2.png) [@jkarippery](https://community.neo4j.com/u/jkarippery)\
**Post date:** [August 11, 2021, 3:05pm UTC](https://community.neo4j.com/t/how-return-list-of-node-id-from-shortest-path/42751/3 "2021-08-11T15:05:11Z")

</div>

> [@abk](#):
>
> `RETURN [x in nodes(p) | id(x)]`

Thank you abk,  
I need one more help,  
How to return their weight (distance) value in a list

---

<div class="post-metadata">

**Author:** ![akollegger1](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/akollegger1/32/14828_2.png) [@akollegger1](https://community.neo4j.com/u/akollegger1)\
**Post date:** [August 11, 2021, 7:59pm UTC](https://community.neo4j.com/t/how-return-list-of-node-id-from-shortest-path/42751/4 "2021-08-11T19:59:43Z")

</div>

I'm unsure what you mean by weight or distance. Is that a property on the nodes?

In general, you can pluck whatever you need to get from the object in the second part of the list comprehension, following the `|` character.

Walking through the syntax of the list comprehension:

```auto
RETURN [x in nodes(p) | id(x)]

```

1. `x in` means that x will receive a sequence of values
2. `nodes(p)` provides the sequence, which here will be the nodes of the path `p`
3. `|` is a separator. The left-hand side of will "bind" values that are passed to the right-hand side
4. `id(x)` could be any expression. Here we know `x` is a node, so we're asking for that node's id

So, to get a property from the node instead, do this:

```auto
RETURN [x in nodes(p)) | x.name]

```

Does that help?

-ABK

---

<div class="post-metadata">

**Author:** ![jkarippery](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/jkarippery/32/5653_2.png) [@jkarippery](https://community.neo4j.com/u/jkarippery)\
**Post date:** [August 11, 2021, 8:19pm UTC](https://community.neo4j.com/t/how-return-list-of-node-id-from-shortest-path/42751/5 "2021-08-11T20:19:52Z")

</div>

weight(distance/cost) is a property Keys of the graph,  
here I will explain a example:

```auto
CREATE (a:Location {name: 'A'}),
(b:Location {name: 'B'}),
(c:Location {name: 'C'}),
(h:Location {name: 'H'}),
 (j:Location {name: 'J'}),
(a)-[:ROAD {cost: 50}]->(b),
(a)-[:ROAD {cost: 50}]->(c),
(c)-[:ROAD {cost: 40}]->(j),
(j)-[:ROAD {cost: 30}]->(h),
(h)-[:ROAD {cost: 50}]->(b);

```

Query

```auto
MATCH p=(o{name:"A"})-[r*]->(x{name:"B"})

RETURN [x in nodes(p) | id(x)]

```

path

1. [A,C,J,H,B]
2. [A,B]

**expected output**

```auto
path cost
1. [A,C,J,H,B] [0,50,90,130,180]
2. [A,B] [0,50]

```

Thank you in advance

---

<div class="post-metadata">

**Author:** ![akollegger1](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/akollegger1/32/14828_2.png) [@akollegger1](https://community.neo4j.com/u/akollegger1)\
**Post date:** [August 11, 2021, 9:17pm UTC](https://community.neo4j.com/t/how-return-list-of-node-id-from-shortest-path/42751/6 "2021-08-11T21:17:36Z")

</div>

OK, so you'd like to get values from the relationships rather than the nodes, and accumulate them along the way? That's more of a reduce/fold on an array which produces a new array, rather than a map function.

So let's try reduce:

```auto
RETURN reduce(costs=[0], x in relationships(p) | costs + [costs[size(costs) - 1] + x.cost])

```

1. initialize an accumulator with an array containing a single number `0`
2. bind the relationships in `p` to `x`
3. concatenate the accumulator with a single element array containing a value calculated by combinging the last element of the accumulator with the current cost

Though if you just want the total cost of the path you could simplify to:

```auto
RETURN reduce(total=0, x in relationships(p) | total + x.cost)

```

Closer?

Best,  
ABK

ps. The cypher manual has a lot of good information about lists. Check out:

- [Lists - Cypher Manual](https://neo4j.com/docs/cypher-manual/current/syntax/lists/)
- [Operators - Cypher Manual](https://neo4j.com/docs/cypher-manual/current/syntax/operators/#query-operators-list)
