我有一组用户名(例如['abc','def','ghi'])要添加到图表中的"用户"标签下.
现在我首先要检查用户名是否已经存在(g.V().hasLabel('user').has('username','def')),然后仅添加"用户"标签下用户名属性不匹配的用户名.
此外,这可以在单个gremlin查询或groovy脚本中完成吗?
我正在使用titan graph数据库,tinkerpop3和gremlin REST服务器.
如果边缘尚不存在,我找到以下代码来创建边缘。
g.V().hasLabel("V1")
.has("userId", userId).as("a")
.V().hasLabel("V1").has("userId", userId2)
.coalesce(
bothE("link").where(outV().as("a")),
addE("link").from("a")
)
Run Code Online (Sandbox Code Playgroud)
它工作正常,但我想创建顶点和边(如果它们不存在于 1 个查询中)。
我用新图尝试以下代码,它只是创建新顶点,但它们之间没有关系。
g.V().hasLabel("V1")
.has("userId", userId).fold()
.coalesce(
unfold(),
addV("V1").property("userId", userId1)
).as("a")
.V().hasLabel("V1").has("userId", userId2).fold()
.coalesce(
unfold(),
addV("V1").property("userId", userId2)
)
.coalesce(
bothE("link").where(outV().as("a")),
addE("link").from("a")
)
Run Code Online (Sandbox Code Playgroud)