简体   繁体   English

Neo4j可选比赛

[英]Neo4j Optional Match

I am trying to generate an array which contains the number of messages sent from a employee to all (if any or zero if there is no communication) using OPTIONAL MATCH Clause, but its not working. 我试图生成一个数组,其中包含使用OPTIONAL MATCH子句从员工发送给所有人的消息数(如果有,则为零;如果没有通信,则为零),但是不起作用。 I am not getting zeroes in the array when there is no communication. 没有通信时,数组中没有零。 I am using the following query: 我正在使用以下查询:

MATCH(e:Employee{key:101,div:'finance'}),(b:Employee)
OPTIONAL MATCH (e)-[r:Message]->(b)
WITH e.name as  em, r.NUMBER_OF_MESSAGES as msg 
ORDER BY msg DESC
RETURN em, COLLECT(msg) AS row

Here is the output: 这是输出:

Row: [31,20,12,10,8,7,7,2,2,2,2,1,1,1]

What am I doing wrong here? 我在这里做错了什么? Thanks in advance. 提前致谢。

The problem here is that collect() will collect all values ignoring nulls . 这里的问题是collect()将收集所有值, 而忽略null When has no communication between two nodes the value of msg variable will be null and consequently ignored by COLLECT() . 当两个节点之间没有通信时, msg变量的值将为null ,因此将被COLLECT()忽略。 To fix it change your query to: 要解决此问题,请将您的查询更改为:

MATCH(e:Employee{key:101,div:'finance'}),(b:Employee)
OPTIONAL MATCH (e)-[r:Message]->(b)
WITH e.name as  em, coalesce(r.NUMBER_OF_MESSAGES,0) as msg 
ORDER BY msg DESC
RETURN em, COLLECT(msg) AS row

This query use the coalesce() function. 该查询使用coalesce()函数。 This function will return the first non-null value in the list of arguments passed to it. 此函数将返回传递给它的参数列表中的第一个非空值。 Then, if r.NUMBER_OF_MESSAGES is null it will be replaced by zero. 然后,如果r.NUMBER_OF_MESSAGESnull则它将替换为零。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM