且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Azure Cosmos DB SQL-如何对内部json属性进行转义

更新时间:2023-01-20 17:31:26

PreTrigger,因为您的数据是由设备发送的,因此无法通过PreTrigger

PreTrigger in cosmos db need to be defined in the code, since your data is sent by the device, so it won't work through PreTrigger.

因此,正如您在评论中提到的那样,建议您使用Azure Function CosmosTrigger处理每个文档,然后再将其插入cosmos db.

So, as you mentioned in your comment ,I suggest you using Azure Function CosmosTrigger to process per document before it is inserted into cosmos db.

我的样本文档:

我的Azure Function CosmosTrigger代码:

My Azure Function CosmosTrigger code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Client;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace ProcessJson
{
    public class Class1
    {
        [FunctionName("DocumentUpdates")]
        public static void Run(
        [CosmosDBTrigger("db", "item", ConnectionStringSetting = "myCosmosDB")]
        IReadOnlyList<Document> documents,
        TraceWriter log)
        {
            String endpointUrl = "***";
            String authorizationKey = "***";
            String databaseId = "db";
            String collectionId = "item";

            DocumentClient client = new DocumentClient(new Uri(endpointUrl), authorizationKey); ;

            Document doc = documents[0];

            string gateway = doc.GetPropertyValue<string>("gateway");
            JObject o = JObject.Parse(gateway);

            doc.SetPropertyValue("gateway",o);

            client.ReplaceDocumentAsync(UriFactory.CreateDocumentUri(databaseId, collectionId, doc.Id), doc);

            log.Verbose("document Id " + doc.Id);
        }
    }
}

插入结果: