0

I'm new to Gremlin and CosmosDB. I've been following the tinkerpop tutorials and am using the TinkerFactory.createModern() test graph.

enter image description here

What I am looking for is to return a graphson object similar to this from cosmosdb.

{
"user": {
    "name": "Marko",
    "age": 29       
},
"knows": [
    {"name": "josh", "age": 32},
    {"name": "vadas", "age": 27}
],
"created": [
    {"name": "lop", "lang": "java"} 
]
}

My thoughts were to try

g.V().has('name', 'marko').as('user').out('knows').as('knows').out('created').as('created').select('user', 'knows', 'created')

What i really get back is in the picture below. I was hoping to have single user object, with an array of knows objects and software objects.

If this is possible can you please explain what steps need to be used to get this format.

enter image description here

Hope my question is clear and thanks to anyone that can help =)

Coel Drysdale
  • 193
  • 2
  • 12

1 Answers1

1

You should use project():

gremlin> g.V().has('person','name','marko').
......1>   project('user','knows','created').
......2>     by(project('name','age').by('name').by('age')).
......3>     by(out('knows').project('name','age').by('name').by('age')).
......4>     by(out('created').project('name','lang').by('name').by('lang'))
==>[user:[name:marko,age:29],knows:[name:vadas,age:27],created:[name:lop,lang:java]]

That syntax should work with CosmosDB. In TinkerPop 3.4.0, things get a little nicer as you can use valueMap() a bit more effectively (but I don't think that CosmosDB supports that as of the time of this answer):

gremlin> g.V().has('person','name','marko').
......1>   project('user','knows','created').
......2>     by(valueMap('name','age').by(unfold())).
......3>     by(out('knows').valueMap('name','age').by(unfold())).
......4>     by(out('created').valueMap('name','lang').by(unfold()))
==>[user:[name:marko,age:29],knows:[name:vadas,age:27],created:[name:lop,lang:java]]
stephen mallette
  • 45,298
  • 5
  • 67
  • 135
  • Here are the available CosmosDb commands for gremlin [link](https://learn.microsoft.com/en-us/azure/cosmos-db/gremlin-support#gremlin-steps) – Coel Drysdale Feb 25 '19 at 21:28
  • Thanks so much! I managed to figure out how to return the other person to an array. eg) knows: [{name: vadas,{name:josh}] using the `.fold()` method. `.by(out('knows').project('name','age').by('name').by('age')**.fold()**)` – Coel Drysdale Feb 25 '19 at 21:31
  • i've found that link to be a decent reference but it is not up to date. like for instance, i've been told that `sideEffect()` is supported, but it's not on that page. i think there are other situations like that. oh, and, sorry - i should have added `fold()` - my mistake. – stephen mallette Feb 26 '19 at 11:49