首页 文章

按Azure DocumentDB中的Id访问资源

提问于
浏览
7

我刚刚开始使用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();
    }
}

然后有时候我想创建一个Collection:

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' . 我没有'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资源?”

5 回答

  • 8

    我建议你查看代码示例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()方法查找数据库 .

    希望有所帮助 .

  • 8

    2014年其他答案中公布的信息现在已经过时了 . 可以通过Id直接寻址:

    虽然 _selflinks 仍然存在,并且可用于访问资源,但Microsoft已经添加了一种更简单的方法来查找资源,而不需要您保留 _selflink

    UriFactory UriFactory.CreateDocumentCollectionUri(databaseId,collectionId))UriFactory.CreateDocumentUri(databaseId,collectionId,“document id”);

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

    // 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);
    

    来自 August 13th 2015 的公告可以在这里找到:

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

  • 1

    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;
    
  • -1

    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" });
    
  • 1

    要删除分区集合中的文档,请使用以下格式:

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

相关问题