编辑Azure DocumentDb中所有文档中的字段

时间:2017-03-30 08:56:57

标签: c# azure azure-cosmosdb azure-functions

我在DocumentDb中有我的数据。

我想用plotOptions: { column: { dataLabels: { enabled: true, formatter: function(){ var seriesData=this.series.data; var total=0; for(var i=0;i<seriesData.length;i++){ total+=seriesData[i].y } return (100 * this.y / total).toFixed(2) + "%"; } } } }, 编辑所有文档中的一个字段。

我怎么做?

  1. 为其创建控制台应用
  2. 使用Azure功能
  3. 我尝试了第二个选项但它无效。

    Type == 8
    你知道吗?

1 个答案:

答案 0 :(得分:0)

下面的代码似乎天真且RU浪费但会起作用。

DocumentClient _documentDbclient;

// Database configuration
static readonly string DocumentDbKeyConfig = "3dzS7T3a8lgEblkEnzSsdfasdfasf2omI1cp7==";
static readonly string DocumentDbEndPointConfig = "https://your-docdb.documents.azure.com:443/";
static readonly string DocumentDbDatabaseNameConfig = "your-database-name";


void Main()
{

    string collectionName = "TestCollection";

    Uri uri = UriFactory.CreateDocumentCollectionUri(DocumentDbDatabaseNameConfig, collectionName);
    InitializeDocumentDbAsync(DocumentDbDatabaseNameConfig).ConfigureAwait(false);

    createDocumentCollectionIfNotExistAsync(DocumentDbDatabaseNameConfig, collectionName).ConfigureAwait(false);

    DocumentCollection collection = _documentDbclient.ReadDocumentCollectionAsync(uri).Result;
    var query = _documentDbclient.CreateDocumentQuery(uri);
    var queryList = query.ToList();

    int index = 0;
    int count = 50; // number of item to fetch at a time
    while (true)
    {
        var result = queryList.Skip(index * count).Take(count);

        if (!result.Any())
        {
            // end iterating.
            return;
        }

        foreach (Document element in result)
        {
            JObject j = (dynamic)element;
            if(j["Type"] != 8) continue;

            j["YourProperty"] = "Some Value"; // edit value.
            var upsertedResult = _documentDbclient.UpsertDocumentAsync(uri, j, null, true).Result;
        }
        ++index;
    }
}

public async Task InitializeDocumentDbAsync(string databaseName)
{
    // Create a new instance of the DocumentClient
    _documentDbclient = new DocumentClient(
        new Uri(DocumentDbEndPointConfig), DocumentDbKeyConfig);
    // Check if the database FamilyDB does not exist

    try
    {
        await _documentDbclient.ReadDatabaseAsync(UriFactory.CreateDatabaseUri(databaseName));
    }
    catch (DocumentClientException de)
    {
        // If the database does not exist, create a new database
        if (de.StatusCode == HttpStatusCode.NotFound)
        {
            await _documentDbclient.CreateDatabaseAsync(new Database { Id = databaseName });
        }
        else
        {
            throw;
        }
    }
    catch (Exception e)
    {
        throw;
    }
}

private async Task createDocumentCollectionIfNotExistAsync(string databaseName, string collectionName)
{
    try
    {
        await
            _documentDbclient.ReadDocumentCollectionAsync(UriFactory.CreateDocumentCollectionUri(databaseName, collectionName));
    }
    catch (DocumentClientException de)
    {
        // not exist
        throw;
    }
    catch (Exception e)
    {
        // do nothing.
    }
}