Query Documents in CosmosDB using C#

14,303

Solution 1

Is there a way to write the Azure Function locally in Visual Studio and integrate it into my solution?

Yes, we can write the Azure Function locally in Visual Studio and integrate it into our VS solution. About how to do it, we can refer to: Azure Functions Tools for Visual Studio

I'm open to any way to query CosmosDB documents from code (C# with JSON perhaps?) in Visual Studio

Here is a simple demo for your reference:

    public static List<T> QueryAllDocument(string Uri, string Key, string DatabaseName, string CollectionName)
    {
        DocumentClient client = new DocumentClient(new Uri(Uri), Key, new ConnectionPolicy { EnableEndpointDiscovery = false });
        List<T> list = client.CreateDocumentQuery<T>(UriFactory.CreateDocumentCollectionUri(DatabaseName, CollectionName).ToString(), new SqlQuerySpec("SELECT * FROM c"), new FeedOptions { MaxItemCount = -1 }).ToList<T>();
        return list;
    }

More information for your reference:

Azure Cosmos DB: .NET examples for the SQL API

SQL queries for Azure Cosmos DB

Solution 2

If you don't want to use the .NET SDK that Microsoft provides then, there is a C# library called Cosmonaut which can make it really easy for you to query CosmosDB in your application.

You can do your querying with Cosmos' SQL

var user = await cosmoStore.QueryMultipleAsync<dynamic>("select * from c w.Firstname = 'Smith'");

But it also support model mapping and async querying for entities.

Disclaimer: I'm the creator of Cosmonaut

Share:
14,303
Josh K.
Author by

Josh K.

Updated on June 05, 2022

Comments

  • Josh K.
    Josh K. about 2 years

    I have several documents stored in Azure CosmosDB databases, and I'd like to query them from my application. My application is written in C#, and I want to use C# for the query.

    Right now, I can do this using an Azure Function that holds the query and then connects to my program (I connect to the Web App hosting the Azure Function), but I'm not fully satisfied with this solution, mostly because I had to create the Azure Function through the portal's GUI. Is there a way to write the Azure Function locally in Visual Studio and integrate it into my solution? Alternatively, I'm open to any way to query CosmosDB documents from code (C# with JSON perhaps?) in Visual Studio.

  • Josh K.
    Josh K. about 6 years
    Do I need to have a class for TestEntity? Right now, I just have the data in CosmosDB - is there a way to query it without having a model (e.g. have List<dynamic> or something of that nature)?
  • David Makogon
    David Makogon about 6 years
    The original question is basically a yes/no question, as written; please don't post a generic example, as it doesn't answer a specific question. There is a considerable amount of documentation (and samples) already published. But your example doesn't answer any specific programming question.
  • Lee Liu
    Lee Liu about 6 years
    @JoshK. Yes, we can use dynamic to replace the specify model, as a better way, we can use the generic method,I have updated my answer.
  • Lee Liu
    Lee Liu about 6 years
    @DavidMakogon Thank you for your suggestion, i have modified my answer and provide the documentations.