0

I noticed that when querying items in a Cosmos DB database, the code sample on Microsoft Docs doesn't require a partitionKey. Why is that? I can certainly imagine scenarios where we may want to run a query on the whole database but whenever possible wouldn't it be more efficient and faster to limit the query to a particular partition?

Here's the code from the Docs:

string query = "SELECT * FROM products p WHERE p.category = @category"

var query = new QueryDefinition(query)
  .WithParameter("@category", "gear-surf-surfboards");

using FeedIterator<Product> feed = container.GetItemQueryIterator<Product>(
    queryDefinition: query
);

Here's the link to Microsoft Docs: https://learn.microsoft.com/en-us/azure/cosmos-db/nosql/quickstart-dotnet?pivots=devcontainer-codespace#query-items

2
  • If you look at the source code here - learn.microsoft.com/en-us/azure/cosmos-db/nosql/…, you will notice that the partition key for the container is category id so the query is making use of the partition key. Commented Oct 17, 2024 at 3:23
  • I did notice that but that's just a carefully crafted example, isn't it? partitionKey is not defined in the query. In the example, the query parameter just happened to be the property used as partitionKey. The query parameter could've been some other property that is not the partition key for the container. For example, it could've been string query = "SELECT * FROM products p WHERE p.productName = @productName" Commented Oct 17, 2024 at 5:33

0

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.