Project Into New object with group.

31 views
Skip to first unread message

Mike Dobson

unread,
Dec 6, 2017, 5:11:36 PM12/6/17
to Gremlin-users

Here is my simple code: 

g.addV('person').property('name', 'Mike').as('mike')
.addV('class').property('name', 'math').property('aval', 'true').as('math')
.addV('class').property('name', 'bio').property('aval', 'false').as('bio')
.addE('takes').from('mike').to('math').property('TA', 'true').property('Role', 'Admin')
.addE('takes').from('mike').to('bio').property('TA', 'false').property('Role', 'Help')


My query is:

g.V().hasLabel('person').limit(1).project('Takes').by(outE('takes').group().by().by(valueMap('Role', 'TA')))

Output:

[{
"Takes": {
"e[abf8c9fd-daec-4d12-a630-6f8903146349]080366ec-2fa1-4f30-9574-bb66cd70324f(person)-takes->db088937-bea1-4cd1-b6e8-193dff7f22ff(class)": {
"Role": "Admin",
"TA": "true"
},
"e[c22f0ae3-81bf-4ad5-a7f9-8f75db940cb8]080366ec-2fa1-4f30-9574-bb66cd70324f(person)-takes->db306807-7639-4000-b272-bbd5fef5e170(class)": {
"Role": "Help",
"TA": "false"
}
}
}]

But what i need is:

[{
"Takes": {
{
"Role": "Admin",
"TA": "true"
},
{
"Role": "Help",
"TA": "false"
}
}
}]

Basically i want to get properties from all 'takes' edges, project them as an array to 'Takes' property. I need to figure out how to get rid of the grouping header and just return an array of props.

Thanks.


Stephen Mallette

unread,
Dec 6, 2017, 7:21:38 PM12/6/17
to Gremlin-users
If all you need is the properties from the "takes" edges, then i'm not sure why you need group() at all. isn't it just:

gremlin> g.V().hasLabel('person').limit(1).project('takes').by(outE('takes').valueMap('Role', 'TA').fold())
==>[takes:[[Role:Admin,TA:true],[Role:Help,TA:false]]]


--
You received this message because you are subscribed to the Google Groups "Gremlin-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gremlin-users/ee438e74-a2be-4f56-9594-59a91d505dac%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Mike Dobson

unread,
Dec 7, 2017, 12:39:28 PM12/7/17
to Gremlin-users
Stephen, thank you. 

Can you explain significance of valueMap('Role', 'TA').fold() option. 

Why if "fold()" is omitted, we only get one "takes" edge result. 

Thanks. 
To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-user...@googlegroups.com.

Stephen Mallette

unread,
Dec 7, 2017, 12:53:05 PM12/7/17
to Gremlin-users
It's just a "feature" of the language. The inner traversal is iterated with next() when executed so you only get the first item out. By adding fold() you iterate the traversal to a List first and then next() out that list. 

To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gremlin-users/fe4aaa15-c2e6-4174-b5b1-4c10712631b4%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages