Mixing Gremlin with DocDB API in CosmosDB

One of the best things about CosomosDB is the ability to mix and match APIs and models based on what makes sense for your use case.

Sometimes it just makes sense that your database is built as a graph, and in that case you can use the (familiar) Gremlin language to write your queries. However, for some things it might be easier or more optimal to use familiar DocDB APIs.

One of the best examples of mixing APIs, is for adding / updating your graph. The gremlin syntax for adding vertices or edges is pretty complicated As I’ve presented in the [last] article, we know that in Cosmos, regardless of the API we’re using the data is ultimately stored as documents. So you can use the Insert/Upsert API to add data to your graph rather than the more complicated Gremlin syntax:

Let’s consider a simple person model. To insert a new person with Gremlin, we’d have generate this statement from our class:

g.addV('person').property('firstName', 'Thomas').property('lastName', 'Andersen').property('age', 44).property('PartitionKey','123')

That’s not extremely complicated, but it’s not going to be very clean and extensible either.
However, if we serialize the model to JSON and customize it to match the graphSON format then we can use the straight forward DocumentDB API to insert the node.

Share Comments