【问题标题】:Access resources by Id in Azure DocumentDB在 Azure DocumentDB 中按 Id 访问资源
【发布时间】:2014-08-25 16:57:38
【问题描述】:

我刚开始玩 Azure DocumentDB,我的兴奋变成了困惑。这件事很奇怪。似乎所有内容(数据库、集合、文档)都不需要通过其 ID 访问,而是通过其“SelfLink”访问。例如:

我创建了一个数据库:

public void CreateDatabase()
{
    using (var client = new DocumentClient(new Uri(endpoint), authKey))
    {
        Database db = new Database()
        {
            Id = "TestDB",
        };
        client.CreateDatabaseAsync(db).Wait();
    }
}

然后以后某个时候我想创建一个收藏:

public void CreateCollection()
{
    using (var client = new DocumentClient(new Uri(endpoint), authKey))
    {
        DocumentCollection collection = new DocumentCollection()
        {
            Id = "TestCollection",
        };
        client.CreateDocumentCollectionAsync(databaseLink: "???", documentCollection: collection).Wait();
    }
}

当我真正希望给它的是我的数据库 ID 时,api 想要一个“数据库链接”。我手边没有'databaseLink'。 DocumentDB 真的希望我每次想要做任何事情时都拉下所有数据库的列表并在其中搜索 databaseLink 吗?

这个问题一直存在。如果没有集合的“链接”,我无法将文档保存到集合中。

public void CreateDocument()
{
    using (var client = new DocumentClient(new Uri(endpoint), authKey))
    {
        client.CreateDocumentAsync(documentCollectionLink: "???", document: new { Name = "TestName" }).Wait();
    }
}

因此,要保存文档,我需要集合的链接。要获得收藏链接,我需要数据库链接。要获得数据库链接,我必须拉下我帐户中所有数据库的列表并对其进行筛选。然后我必须使用我发现的那个数据库链接来拉下该数据库中的集合列表,然后我必须筛选以查找我想要的集合的链接。这似乎不对。

我错过了什么吗?我不明白如何使用它吗?当 DocumentDB 坚持使用自己的链接方案来识别所有内容时,为什么我要为我的所有资源分配 ID?我的问题是“如何通过 ID 访问 DocumentDB 资源?”

【问题讨论】:

标签: azure azure-cosmosdb


【解决方案1】:

2014 年其他答案中发布的信息现在有些过时了。可以通过 Id 直接寻址:

虽然 _selflinks 仍然存在,并且可用于访问资源,但 Microsoft 已经添加了一种更简单的方法来通过其 Id 定位资源,不需要您保留 _selflink:

UriFactory

UriFactory.CreateDocumentCollectionUri(databaseId, collectionId))

UriFactory.CreateDocumentUri(databaseId, collectionId, "document id");

这使您能够创建一个安全的 Uri(例如允许空格) - 它在功能上与资源 _selflink 相同;微软公告中给出的示例如下所示:

// Use **UriFactory** to build the DocumentLink
Uri docUri = UriFactory.CreateDocumentUri("SalesDb", "Catalog", "prd123");

// Use this constructed Uri to delete the document
await client.DeleteDocumentAsync(docUri);

该公告于 2015 年 8 月 13 日发布,可在此处找到:

https://azure.microsoft.com/en-us/blog/azure-documentdb-bids-fond-farewell-to-self-links/

【讨论】:

    【解决方案2】:

    我建议您查看代码示例 here,尤其是 DocumentDB.Samples.ServerSideScripts 项目。

    在 Program.cs 中,您将找到 GetOrCreateDatabaseAsync 方法:

    /// <summary> 
    /// Get or create a Database by id
    /// </summary> 
    /// <param name="id">The id of the Database to search for, or create.</param> 
    /// <returns>The matched, or created, Database object</returns> 
    private static async Task<Database> GetOrCreateDatabaseAsync(string id) 
    { 
        Database database = client.CreateDatabaseQuery()
            .Where(db => db.Id == id).ToArray().FirstOrDefault(); 
        if (database == null) 
        { 
            database = await client.CreateDatabaseAsync(
                new Database { Id = id }); 
        } 
    
        return database; 
    } 
    

    要回答您的问题,您可以使用此方法通过其 ID 和其他资源(集合、文档等)使用它们各自的 Create[ResourceType]Query() 方法来查找您的数据库。

    希望对您有所帮助。

    【讨论】:

    【解决方案3】:

    create database 调用返回一个数据库对象:

    var database = client.CreateDatabaseAsync(new Database { Id = databaseName }).Result.Resource;
    

    然后你可以用它来创建你的收藏

    var spec = new DocumentCollection { Id = collectionName };
    spec.IndexingPolicy.IndexingMode = IndexingMode.Consistent;
    spec.IndexingPolicy.Automatic = true;
    spec.IndexingPolicy.IncludedPaths.Add(new IndexingPath { IndexType = IndexType.Range, NumericPrecision = 6, Path = "/" });
    var options = new RequestOptions
    {
        ConsistencyLevel = ConsistencyLevel.Session
    };
    
    var collection = client.CreateDocumentCollectionAsync(database.SelfLink, spec, options).Result.Resource;
    

    【讨论】:

      【解决方案4】:

      client.Create... 方法返回具有您正在寻找的自链接的对象

      Database database = await client.CreateDatabaseAsync(
          new Database { Id = "Foo"});
      DocumentCollection collection = await client.CreateDocumentCollectionAsync(
          database.SelfLink, new DocumentCollection { Id = "Bar" });
      Document document = await client.CreateDocumentAsync(
          collection.SelfLink, new { property1 = "Hello World" });
      

      【讨论】:

        【解决方案5】:

        要删除分区集合中的文档,请利用此格式:

        result = await client.DeleteDocumentAsync(selfLink, new RequestOptions {
            PartitionKey = new PartitionKey(partitionKey)
        });
        

        【讨论】:

          猜你喜欢
          • 2017-09-18
          • 1970-01-01
          • 2023-04-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-03-26
          • 1970-01-01
          • 2019-10-11
          相关资源
          最近更新 更多