cosmos-db
  1. cosmos-db-key-value

Key-value (CosmosDB Data Models)

Key-value is one of the CosmosDB data models that allow you to store and retrieve data using simple key-value pairs. In this model, the data is stored as a collection of key-value pairs, where each key represents a unique identifier for the corresponding value.

Syntax

The syntax for storing a key-value pair in CosmosDB is as follows:

{
    "key": "mykey", 
    "value": { 
        "attribute1": "value1", 
        "attribute2": "value2"
    }
}

Here, "key" is the unique identifier for the value, and "value" is the data associated with the key. The "value" can contain multiple attributes with their respective values.

Example

Let's consider an example where we want to store the details of a user using the key-value data model in CosmosDB.

{
    "key": "user001",
    "value": {
        "name": "John Doe",
        "email": "johndoe@example.com",
        "age": 30,
        "country": "USA"
    }
}

Output

When the above key-value pair is stored in CosmosDB, it looks like the following:

{
    "_rid": "EbQXAL1GJAcBAAAAAAAAAA==",
    "key": "user001",
    "value": {
        "name": "John Doe",
        "email": "johndoe@example.com",
        "age": 30,
        "country": "USA"
    }
}

Here, "_rid" is the resource id generated by CosmosDB, which is used to uniquely identify the stored data.

Explanation

The key-value data model is a simple and efficient way to store and retrieve data in CosmosDB. Each key-value pair represents a separate entity of the data, allowing for easy access and manipulation of specific values. In addition, the key-value model is highly scalable, making it ideal for storing and retrieving large amounts of data.

Use

The key-value data model is commonly used in situations where fast and efficient access to specific values is needed. This includes storing user profiles, session data, and other similar types of data. Key-value pairs can also be used to store configuration data, such as application settings, which can be easily accessed and modified as needed.

Important Points

  • Each key-value pair represents a separate entity of the data.
  • Keys must be unique.
  • Values can contain multiple attributes.
  • The key-value model is highly scalable.

Summary

The key-value data model is a simple and efficient way to store and retrieve data in CosmosDB. By using key-value pairs to represent data entities, it allows for easy access and manipulation of specific values. The key-value model is highly scalable, making it ideal for storing and retrieving large amounts of data efficiently.

Published on: