Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DocumentDB with Azure Functions

I'm trying to connect an Azure DocumentDB and save documents using Azure Functions but I don't know how to create the connection.

like image 872
Luís Fura Avatar asked Oct 28 '25 09:10

Luís Fura


2 Answers

You can do it using the Azure Portal. After you created the DocumentDB -

  • Create new Azure Function.
  • Go to the Integrate Tab.
  • You can choose Azure Document DB as an output for your function.
  • Choose your Document DB/Database Name/Collection you want to use.
  • Document parameter name is the Output of your function.

For example

using System;

public static void Run(string input, out object document, TraceWriter log)
{
    log.Info($"C# manually triggered function called with input: {input}");
    document = new {
        text = $"I'm running in a C# function! {input}"
    };
}

you need to provide out object which is the same as you defined in the output tab.

like image 199
shachar Avatar answered Oct 30 '25 00:10

shachar


You can just use the document client directly:

var endpoint = "https://XXXXX.documents.azure.com:443/";
var authKey = "XXXXX";

using (var client = new DocumentClient(new Uri(endpoint), authKey))
{
    var sqlCountQuery = "select value count(1) from c";
    IDocumentQuery<dynamic> query = client.CreateDocumentQuery<dynamic>(UriFactory.CreateDocumentCollectionUri("YOUR_DB_ID", "YOUR_COLLECTON_ID"), sqlCountQuery).AsDocumentQuery();
    ....
}
like image 42
Steve Taylor Avatar answered Oct 30 '25 00:10

Steve Taylor



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!