Neo4jClient - 如何检查 属性 是否存在

Neo4jClient - How to check if property exists

我在将以下 Cypher 查询转换为 Neo4jClient 语法时遇到问题。

匹配 n WHERE NOT (HAS (n.User)) 或 n.User = "username" RETURNn

这是我目前添加了一些关系逻辑并省略了 HAS 逻辑的结果

var results = Client.Cypher
               .OptionalMatch("(result)-[connection:Connection]-(result2)")
               .Where((Result result) => result.User == username)
               .Return((result, connection, result2) => new Neo4jResultSingle()
               {
                   SearchedNode = result.As<Node<Result>>(),
                   RelationshipConnection = connection.As<RelationshipInstance<Connection>>(),
                   Relationship = connection.As<RelationshipInstance<ConnectionRelationship>>(),
                   RelationshipedNode = result2.As<Node<Result>>()
               }).Results.ToList();

查看您的原始查询和您的 C# 的组合,也许以下内容可能有所帮助?

var results = Client.Cypher
           .Match("(result)-[connection:Connection]-(result2)")
           .Where("WHERE NOT HAS(n.User) OR n.User = {username}")
           .WithParams(new { username = username })
           .Return((result, connection, result2) => new Neo4jResultSingle()
           {
               SearchedNode = result.As<Node<Result>>(),
               RelationshipConnection = connection.As<RelationshipInstance<Connection>>(),
               Relationship = connection.As<RelationshipInstance<ConnectionRelationship>>(),
               RelationshipedNode = result2.As<Node<Result>>()
           })
           .Results
           .ToList();

这会重写您的 WHERE 子句以包含 HAS 和用户名逻辑,同时对其进行参数化以缓存查询计划并防止注入。

HAS 已被 EXISTS()

取代

来自最新文档:Property existence checking

MATCH (n) WHERE EXISTS(n.User) RETURN n

对于相反的匹配在

之前添加NOT
MATCH (n) WHERE NOT EXISTS(n.User) RETURN n

Neo4j 4.3,

The exists() function has been deprecated for property existence checking and has been superseded by IS NOT NULL.

所以请使用

MATCH (n) 
WHERE n.User IS NOT NULL 
RETURN n

MATCH (n) 
WHERE n.User IS NULL 
RETURN n

具体在您的代码中,

var results = Client.Cypher
       .Match("(result)-[connection:Connection]-(result2)")
       .Where("WHERE n.User IS NULL OR n.User = {username}")
       .WithParams(new { username = username })
       .Return((result, connection, result2) => new Neo4jResultSingle()
       {
           SearchedNode = result.As<Node<Result>>(),
           RelationshipConnection = connection.As<RelationshipInstance<Connection>>(),
           Relationship = connection.As<RelationshipInstance<ConnectionRelationship>>(),
           RelationshipedNode = result2.As<Node<Result>>()
       })
       .Results
       .ToList();