Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
06-16-2020 01:02 AM
UNWIND $test as rows
MATCH (b:Batch)
WHERE all(test in keys(rows) WHERE b[test]=rows[test])
RETURN DISTINCT b{
.*, driver_count: [ (d:Driver)-[:Yes]->(b:Batch) | count(d)]
} as data
I am trying to filter the batch nodes and also count the number of devices that are attached to it simultaneously. So if there are no driver nodes related to the batch node, I want to return the count as zero. However this query gives me an error. If I use MATCH (d:Driver)-[:Yes]->(b:Batch) then I wont take the batch nodes with no relationships
Solved! Go to Solution.
06-16-2020 02:36 AM
UNWIND $test as rows
MATCH (b:Batch)
WHERE all(test in keys(rows) WHERE b[test]=rows[test])
WITH b
OPTIONAL MATCH (d:Driver)-[:Yes]->(b)
RETURN DISTINCT b AS b, count(d) AS counter
If you want all data in the same dictionnary:
UNWIND $test as rows
MATCH (b:Batch)
WHERE all(test in keys(rows) WHERE b[test]=rows[test])
WITH b
OPTIONAL MATCH (d:Driver)-[:Yes]->(b)
WITH DISTINCT b AS b, count(d) AS c
RETURN apoc.map.mergeList([properties(b), {counter:c}])
06-16-2020 01:34 AM
06-16-2020 01:57 AM
Hello @Cobra
UNWIND $test as rows
OPTIONAL MATCH (d:Driver)-[:Yes]->(b:Batch)
WHERE all(test in keys(rows) WHERE b[test]=rows[test])
RETURN DISTINCT b{
.*, driver_count: count(d)
} as data
Actually this was my query when i started but I realized that it wont return the batch nodes that dont have this specific relationship.
I am trying to return all batch nodes. That is why I am using the above query which takes into account for all the batch nodes but that gives me an error since I cant count in the return function.
06-16-2020 01:59 AM
Try WITH
and after RETURN
🙂
06-16-2020 02:12 AM
Sorry I don't get for which query are you referring too. Also I dont get where to implement the WITH clause.
06-16-2020 02:14 AM
On the first query, you said you cannot count in the return, so instead of returning directly, you can add a WITH
clause to count and after RETURN
the result 🙂
06-16-2020 02:32 AM
UNWIND $test as rows
MATCH (b:Batch)
WHERE all(test in keys(rows) WHERE b[test]=rows[test])
WITH b
OPTIONAL match (d:Driver)-[:Yes]->(b:Batch)
RETURN DISTINCT [b,count(d)] as data
This worked for me however i want there to be a name attached to the count.
So when i get the data returned
[{"batch_id":"batch_01","batch_name":"batch","_uuid":"7f8dfc37-e23d-4c│
│9a-b805-c1ec8e084735"},0] there is a 0 at the end with no indication that it is for the count driver
06-16-2020 02:36 AM
UNWIND $test as rows
MATCH (b:Batch)
WHERE all(test in keys(rows) WHERE b[test]=rows[test])
WITH b
OPTIONAL MATCH (d:Driver)-[:Yes]->(b)
RETURN DISTINCT b AS b, count(d) AS counter
If you want all data in the same dictionnary:
UNWIND $test as rows
MATCH (b:Batch)
WHERE all(test in keys(rows) WHERE b[test]=rows[test])
WITH b
OPTIONAL MATCH (d:Driver)-[:Yes]->(b)
WITH DISTINCT b AS b, count(d) AS c
RETURN apoc.map.mergeList([properties(b), {counter:c}])
06-16-2020 02:38 AM
Thanks so much and sorry for the confusion and me being slow.
06-16-2020 02:39 AM
No problem don't worry, I was happy to help
All the sessions of the conference are now available online