cosmos-db
  1. cosmos-db-graph

Graph - CosmosDB Data Model

Introduction

Cosmos DB offers a graph database modeling technique called graph data model. It defines the data model as a collection of nodes (vertices) and edges. In the graph data model, the relationship between entities is defined as edges. Nodes are the individual entities, such as a person, place, or thing.

Syntax

Graph data model allows you to represent complex relationships as a graph of interconnected nodes and edges. The syntax used to define a graph data model in Cosmos DB is as follows:

{
  "id": "vertex-id",
  "label": "vertex-label",
  "property1": "value1",
  "property2": "value2",
  "propertyN": "valueN",
  "_edges": [
    {
      "id": "edge-id",
      "label": "edge-label",
      "_vertexId": "vertex-id",
      "property1": "value1",
      "property2": "value2",
      "propertyN": "valueN"
    },
    {
      "id": "edge-id",
      "label": "edge-label",
      "_vertexId": "vertex-id",
      "property1": "value1",
      "property2": "value2",
      "propertyN": "valueN"
    }
  ]
}

Example

Here is an example of a graph data model in json format:

{
    "id": "1",
    "label": "person",
    "name": "John Smith",
    "location": "Seattle",
    "_edges": [
        {
            "id": "2",
            "label": "knows",
            "_vertexId": "2",
            "since": "2005"
        },
        {
            "id": "3",
            "label": "likes",
            "_vertexId": "3",
            "since": "2010"
        }
    ]
}

Output

The output of the above example will be a graph representation of John Smith and his relationships with other entities he knows and likes.

Graph Representation

Explanation

In the above example, the vertex label is "person" and the vertex has three properties "id", "name", and "location". The vertex has two outgoing edges, "knows" and "likes". The edges are represented as "_edges" jsonArray, each with its properties, including the "id", "label", and "_vertexId" properties. The "_vertexId" property specifies the destination vertex's id to which the edge connects.

Use

The graph data model is useful for modeling complex relationships in the data. It provides a flexible and powerful way to represent data for use cases such as social networks, recommendation engines, and fraud detection systems.

Important Points

  • Graph data model allows representing complex relationships as a graph of interconnected nodes and edges.
  • Nodes are individual entities, such as persons, places, or things, and edges define relationships between them.
  • Graph data model is suitable for use cases such as social networks, recommendation engines, and fraud detection systems.

Summary

Cosmos DB graph data model offers a powerful way to represent data with complex relationships. It allows defining nodes and edges to represent individual entities and relationships between them, respectively. It is a suitable data model for use cases such as social networks, recommendation engines, and fraud detection systems.

Published on: