General Availability: Computed Properties in Azure Cosmos DB for NoSQL

James Codella

We’re thrilled to share that Computed Properties have now hit General Availability in Azure Cosmos DB for NoSQL! Computed properties are a Cosmos DB developer’s dream come true! They allow you to write your query logic just once and then reuse it across multiple queries, saving you time and effort in your database operations. Computed Properties can be added to your indexing policy opening up the possibility to reduce the RU costs and latency of your queries!

What are Computed properties

Computed properties are a feature in Azure Cosmos DB that allow you to derive values from existing item properties. However, these derived values are not stored within the items themselves. They are scoped to a single item and can be used in queries as if they were regular properties. This feature simplifies your work by allowing you to write complex query logic once and use it multiple times. You can also improve performance by adding an index on these properties or incorporating them into a composite index.

There are a few rules to follow when working with computed properties. They must be at the top level in the item and cannot have a nested path. Each computed property definition consists of a name and a query. The name is the identifier for the computed property, and the query is the logic that calculates the property value for each item.

It’s important to note that computed properties are scoped to an individual item. This means they cannot use values from multiple items or depend on other computed properties. To keep things manageable, each container can have up to 20 computed properties.

Here’s an example of a computed property definition you can create.

{
  "computedProperties": [
    {
      "name": "cp_lowerName",
      "query": "SELECT VALUE LOWER(c.name) FROM c"
    }
  ]
}

Creating a Computed Property

Creating Computed Properties requires use of the.NET, Java, or Python SDKs, or the Azure Portal. Once these computed properties have been established, you have the ability to run queries that make reference to these properties. This can be done through any means, encompassing all SDKs and the Data Explorer accessible via the Azure portal.

Supported version Notes
.NET SDK v3 >= 3.34.0-preview Computed properties are currently available only in preview package versions.
Java SDK v4 >= 4.46.0 Computed properties are currently under preview version.
Python SDK >= v4.5.2b5 Computed properties are currently under preview version.

Image Screenshot 2024 05 17 at 7 04 40 AM

Next Steps

The introduction of Computed Properties to Azure Cosmos DB for NoSQL marks a significant enhancement for developers. This feature simplifies database operations by allowing query logic to be written once and reused across multiple queries, thereby saving time and effort. Additionally, by incorporating computed properties into your indexing policy, you can potentially reduce RU costs and query latency. With the ability to derive values from existing item properties without storing them directly, Computed Properties streamline complex query logic and improve performance. Start exploring Computed Properties today to optimize your Azure Cosmos DB operations.

About Azure Cosmos DB

Azure Cosmos DB is a fully managed and serverless distributed database for modern app development, with SLA-backed speed and availability, automatic and instant scalability, and support for open-source PostgreSQL, MongoDB and Apache Cassandra. Try Azure Cosmos DB for free here. To stay in the loop on Azure Cosmos DB updates, follow us on XYouTube, and LinkedIn.

To quickly build your first database, watch our Get Started videos on YouTube and explore ways to dev/test free.

0 comments

Leave a comment

Feedback usabilla icon