Neo4J Cypher:仅当它为空但仍为 return 节点时才设置 属性

Neo4J Cypher: Set property only if it is null but still return the node

我对 Neo4J 还很陌生,并且遇到以下问题:

我只想在 属性 尚不存在(如果它为空)时设置它,而不想在 属性 存在时设置它。 我怎样才能修改下面的查询来实现

  MATCH (u:User)
  WHERE u.uuid=$userId
  SET u.unsubscribedAt = timestamp()
  RETURN u

提前致谢

更新 1: 我忘了提一件事:无论如何我仍然想 return 节点,所以不能在 where 子句

中添加 AND u.unsubscribedAt is null

添加 属性 作为匹配项的鉴别符怎么样?

  MATCH (u:User)
  WHERE u.uuid=$userId AND IS NULL u.unsubscribedAt
  SET u.unsubscribedAt = timestamp()
  RETURN u

您可以使用 coalesce 来实现这一点。它 returns 它传递的第一个非空值,因此如果已经存在,它将解析现有值

  MATCH (u:User)
  WHERE u.uuid=$userId
  SET u.unsubscribedAt = coalesce(u.unsubscribedAt, timestamp())
  RETURN u

如果您不介意 属性 被其原始值覆盖,您也可以这样做:

MATCH (u:User {uuid:$userId})
SET u.unsubscribedAt = COALESCE(u.unsubscribedAt,timestamp())
RETURN u