3

When using the nuget Microsoft.Azure.DocumentDB and its DocumentClient class you can get the compiled query that will be executed against cosmos. Like so:

var documentClient = new DocumentClient(new Uri(endpoint), authKey);

var query = documentClient
    .CreateDocumentQuery<Product>(collectionUrl, queryOptions)
    .Where(x => x.Id == "apple-iphone")
    .Select(x => new
    {
        x.Id
    })
    .AsDocumentQuery();

// Here you can inspect the compiled query: "SELECT VALUE {"Id": root["id"]} FROM root WHERE (root["id"] = "apple-iphone")" 
var rawQuery = query.ToString();

However when using Microsoft.Azure.Cosmos, I dont see any compiled query or how it can be extracted when debugging and inspecting the variables:

var container = new CosmosClientBuilder(endpoint, authKey)
    .WithConnectionModeDirect()
    .WithThrottlingRetryOptions(TimeSpan.FromSeconds(30), 10)
    .Build()
    .GetContainer(databaseId, collectionId);

var query = container
    .GetItemLinqQueryable<Product>(requestOptions: queryOptions)
    .Where(x => x.Id == "apple-iphone")
    .Select(x => new
    {
        x.Id
    })
    .ToFeedIterator();

// How do I get the compiled query here???
var rawQuery = query.ToString();

Is there anyway to get the compiled query that will be executed against cosmos when using Microsoft.Azure.Cosmos?

1 Answer 1

3

Before you call ToFeedIterator(), you can call queryable.ToQueryDefinition().QueryText on the IQueryable. You can also call ToString() directly to get the SQL query definition, just like in v2.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.