第 1 步:请在您的 cosmos db 中创建您的存储过程。
示例存储过程 js 代码:
function sample() {
var collection = getContext().getCollection();
var isAccepted = collection.queryDocuments(
collection.getSelfLink(),
'SELECT * FROM root r where r.id = "1"',
function (err, feed, options) {
if (err) throw err;
if (!feed || !feed.length) {
var response = getContext().getResponse();
response.setBody('no docs found');
}
else {
var a = new Date();
var doc = feed[0];
doc.time = a;
collection.replaceDocument(doc._self,doc,function(err) {
if (err) throw err;
});
var response = getContext().getResponse();
response.setBody(JSON.stringify("update success"));
}
});
if (!isAccepted) throw new Error('The query was not accepted by the server.');
}
第 2 步:创建 c# azure 函数 TimeTrigger。
示例函数代码:
using System;
using Microsoft.Azure.Documents.Client;
public static void Run(TimerInfo myTimer, TraceWriter log)
{
private static DocumentClient client;
static string endpoint = "https://***.documents.azure.com:443/";
static string key = "***";
client = new DocumentClient(new Uri(endpoint), key);
StoredProcedureResponse<bool> sprocResponse = await client.ExecuteStoredProcedureAsync<bool>(
"/dbs/db/colls/coll/sprocs/updatetest/");
if (sprocResponse.Response) log.Info(sprocResponse.Response);
log.Info($"Cosmos DB is updated at: {DateTime.Now}");
}
第 3 步:在 azure-functions 中添加文档数据库程序集引用。
您可以点击Azure函数>查看文件>添加一个名为'project.json'的新文件(如果它不存在)。在此文件中写入以下代码然后点击运行安装包:
{
"frameworks": {
"net46":{
"dependencies": {
"Microsoft.Azure.DocumentDB": "1.20.1"
}
}
}
}
更多细节,你可以参考这个doc。
希望对你有帮助。