Reputation: 3141
if I have a vertex like this:
{
"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"
}
]
}
}
Is it possible to choose properties to return to have an object like the following using gremlin?
{
"id": "1",
"name": "Murilo"
}
Thanks!
Upvotes: 2
Views: 811
Reputation: 46226
I'll use the TinkerPop "modern" toy graph to demonstrate some options:
gremlin> graph = TinkerFactory.createModern()
==>tinkergraph[vertices:6 edges:6]
gremlin> g = graph.traversal()
==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]
You could do something like this:
gremlin> g.V(1).valueMap(true,'name')
==>[label:person,name:[marko],id:1]
but that includes vertex label and wraps the "name" in a list (to account for multi-properties). So, while quick/easy, it's not quite a match for the output you asked for. To get that specific output, I would use project() step which would look like this:
gremlin> g.V(1).project("id","name").by(id).by('name')
==>[id:1,name:marko]
If you have a mix of vertices that are being projected where some may not have certain properties you can use coalesce()
or similar means to ensure a default value:
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]
Upvotes: 4