如果我有这样的顶点:
{
"id": "1",
"label": "user",
"type": "vertex",
"outE": {
"worksAt": [
{
"id": "6e47aa14-0a3a-4e45-8ac4-043ec9f32b50",
"inV": "spaceneedle.com.br"
}
]
},
"properties": {
"name": [
{
"id": "cce42090-efc5-4bb2-9576-922d19164d98",
"value": "Murilo"
}
],
"domain": [
{
"id": "murilo|domain",
"value": "spaceneedle.com.br"
}
]
}
}
是否可以使用gremlin选择要返回的属性以获得如下对象?
{
"id": "1",
"name": "Murilo"
}
谢谢!
答案 0 :(得分:3)
我将使用TinkerPop“现代”玩具图来展示一些选项:
gremlin> graph = TinkerFactory.createModern()
==>tinkergraph[vertices:6 edges:6]
gremlin> g = graph.traversal()
==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]
你可以这样做:
gremlin> g.V(1).valueMap(true,'name')
==>[label:person,name:[marko],id:1]
但是它包括顶点标签并在列表中包装“名称”(以考虑多属性)。因此,虽然快速/简单,但它与您要求的输出并不完全匹配。要获得特定输出,我会使用project() step,如下所示:
gremlin> g.V(1).project("id","name").by(id).by('name')
==>[id:1,name:marko]
如果您正在投影的顶点混合,而某些顶点可能没有某些属性,您可以使用coalesce()
或类似方法来确保默认值:
gremlin> g.V().project('id','name','age').by(id).by('name').by(coalesce(values('age'),constant('none')))
==>[id:1,name:marko,age:45]
==>[id:2,name:vadas,age:27]
==>[id:3,name:lop,age:none]
==>[id:4,name:josh,age:32]
==>[id:5,name:ripple,age:none]
==>[id:6,name:peter,age:35]