Azure DocumentDB-ReplaceDocumentCollectionAsync

时间:2018-11-13 12:47:00

标签: c# azure .net-core azure-cosmosdb

在Azure DocumentDB中,当我使用ReplaceDocumentCollectionAsync时出现以下错误:

  

Microsoft.Azure.Documents.NotFoundException:指定的值“   查询'$ resolveFor'无效。,Windows / 10.0.17134   documentdb-netcore-sdk / 1.8.1,位于   Microsoft.Azure.Documents.DocumentServiceRequest

我正在尝试对现有Collection执行IndextTransformation,这是我的代码:

        var collectionUri = UriFactory.CreateDocumentCollectionUri(_configuration.DatabaseName, _configuration.CollectionName);

        _logger.Information("Perform index transformation");
        _logger.Information("Create a collection with indexing policy");

        var collection = new DocumentCollection { Id = _configuration.CollectionName };
        collection.IndexingPolicy.IncludedPaths.Add(new IncludedPath
        {
            Path = "/*",
            Indexes = new Collection<Index> {
                new RangeIndex(DataType.String) { Precision = -1 },
                new RangeIndex(DataType.Number) { Precision = -1 } }
        });

        await _client.CreateDocumentCollectionIfNotExists(collection, _configuration.DatabaseName);
        _logger.Information("Collection {0} created with index policy \n{1}", collection.Id, collection.IndexingPolicy);

        _logger.Information("Change the collection's indexing policy, and then do a replace operation on the collection");
        collection.IndexingPolicy.IndexingMode = IndexingMode.Consistent;
        await _client.ReplaceDocumentCollectionAsync(collection);

        _logger.Information("Check progress and wait for completion - should be instantaneous since we have only a few documents, but larger collections will take time...");
        await WaitForIndexTransformation(collection);

1 个答案:

答案 0 :(得分:0)

您需要从具有所有选项填充的create方法中获取Collection,然后对其进行操作。看起来像这样:

var collectionUri = UriFactory.CreateDocumentCollectionUri(_configuration.DatabaseName, _configuration.CollectionName);

_logger.Information("Perform index transformation");
_logger.Information("Create a collection with indexing policy");

var collection = new DocumentCollection { Id = _configuration.CollectionName };
collection.IndexingPolicy.IncludedPaths.Add(new IncludedPath
{
    Path = "/*",
    Indexes = new Collection<Index> {
        new RangeIndex(DataType.String) { Precision = -1 },
        new RangeIndex(DataType.Number) { Precision = -1 } }
});

var createdCollection = await _client.CreateDocumentCollectionIfNotExists(collection, _configuration.DatabaseName);
_logger.Information("Collection {0} created with index policy \n{1}", collection.Id, collection.IndexingPolicy);

_logger.Information("Change the collection's indexing policy, and then do a replace operation on the collection");
createdCollection.IndexingPolicy.IndexingMode = IndexingMode.Consistent;
await _client.ReplaceDocumentCollectionAsync(createdCollection);

_logger.Information("Check progress and wait for completion - should be instantaneous since we have only a few documents, but larger collections will take time...");
await WaitForIndexTransformation(createdCollection);