gremlin-both()与bothE().bothV()的区别

时间:2022-07-22
本文章向大家介绍gremlin-both()与bothE().bothV()的区别,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

前言

之前一直以为在gremlin查询中,gremlin的both()和bothE().bothV()效果相同。但是在实际应用中,发现他们并不是相同的。

let`s begin~ =.=

graph.V(3).both(): 返回 id为3的节点出边和入边获取到的目标节点,不包含id为3的源节点 graph.V(3).bothE().bothV():返回 id为3的节点的出边和入编获取到的所有节点,包含id为3的源节点

官网上解释

bothV() The bothV step returns the vertices at both ends of an edge

both() If we wanted to return vertices instead of edges, we could use the both step. This will return all of the vertices connected to the vertex with an ID of 3 regardless of whether they are connected by an outgoing or an incoming edge.

官网链接 http://kelvinlawrence.net/book/Gremlin-Graph-Guide.html#otherv

实际执行测试结果

ps:以下结果均在gremlin服务中实际测试,不过进行了信息脱敏处理

获取测试数据,执行:g.V().has("user_id","5796").bothE("edge_value")获取一条边如下,源节点(id为2539) 和 目标节点(id为8853)

==>e[2l8xqf8-15zryu8-5slx][2539-edge_value->8853]

使用both执行:g.V().has("user_id","5796").both("edge_value") 获取到一个节点,只包含目标节点,不包含源节点

==>v[8853]

使用bothE、bothV执行:g.V().has("user_id","5796").bothE("edge_value").bothV() 获取到两个节点,可以发现不仅包含目标节点还包含源节点

==>v[2539] ==>v[8853]

over~ =.=