2
votes

What I would like to be able to do:

  • Save schemaless JSON to documents
  • Connect those documents arbitrarily
  • Get recursive tree of documents based on aforementioned connections, like for example:
{
    "name": "Document 1",
    "includes": [
        {
            "name": "Document 2.1"
            "includes": [
                {
                    "name": "Document 3",
                    "includes": []
                }
            ]
        },
        {
            "name": "Document 2.2",
            "includes": []
        }
    ]
}

Current status of my setup:

  • CosmosDB instance configured with Graph (Gremlin) API
  • Possible to create (JSON) documents through DocumentDB API
  • Possible to created edges to documents through Graph API
  • Using Node.js SDKs

Questions:

  1. Is it possible to save JSON objects as vertices through Graph API? It allows creating vertices with g.addV('person').property('id', 'thomas').property('firstName', 'Thomas').property('age', 44).property('userid', 1) but something like g.addV({ firstName: 'Thomas' }) does not seem to work.

  2. If I add documents through DocumentDB API and edges between them through Graph API and traverse through the graph, results only include IDs of the documents, not other properties. Is it possible to populate the documents somehow?

Example traversal query:

g.V('03e0576f-2ff7-6109-5ed5-237b43191354').repeat(out('includes')).until(not(out('includes'))).simplePath().dedup().tree().by('id')

Result from this query:

[{
    "03e0576f-2ff7-6109-5ed5-237b43191354": {
        "key": "03e0576f-2ff7-6109-5ed5-237b43191354",
        "value": {
            "7fab4007-c80c-ba21-f5d3-8eb353ea3279": {
                "key": "7fab4007-c80c-ba21-f5d3-8eb353ea3279",
                "value": {
                    "eec55fbd-6900-130d-247f-fb437b093711": {
                        "key": "eec55fbd-6900-130d-247f-fb437b093711",
                        "value": {}
                    },
                    "cfd14a8c-1ac3-6cc3-e2a4-ac3f250478e1": {
                        "key": "cfd14a8c-1ac3-6cc3-e2a4-ac3f250478e1",
                        "value": {
                            "acac136a-3bd4-831c-df6e-e5b95e593b9a": {
                                "key": "acac136a-3bd4-831c-df6e-e5b95e593b9a",
                                "value": {}
                            }
                        }
                    }
                }
            }
        }
    }
}]
1

1 Answers

4
votes

Yes, it is possible to insert documents both through the Graph API and through the Document API. However, Cosmos expects a specific GraphSON format for the documents in order for all of their properties to be picked up during graph traversal.

I'd recommend taking a look at both Vertex Properties and GraphSON from the Tinkerpop documentation to start to get a better idea about these topics.

When adding a document through Gremlin the syntax is a name value comma separated for all properties you want to add. Try this:

g.addV('label', 'human', 'name', 'jesse', 'age', 27)

Now if you go to the Azure portal and execute a SQL query SELECT * FROM c you'll be able to see the format that Cosmos has translated your document into.