2

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 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! Got it (mostly) working now :) One thing thought: Saving documents via createDocument and can't figure out how to save nested data (I guess these are called meta-properties in Gremlin). Trying to save document like this and when querying with Gremlin, address has both values but I'd need a label-field of somekind to tell them apart.
Cosmos does support adding meta properties. I've updated your gist to include the format that Cosmos expects when attaching meta properties through the Document APIs gist.github.com/WonderPanda/a5c1df4df55d431eeb26eb97d4913aa1.
You could also consider setting the Id of the different properties to something human friendly but if you need to nest multiple meta properties then refer to my gist

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.