Documentdb-delete-collection
提供:Dev Guides
DocumentDB-コレクションの削除
1つまたは複数のコレクションをドロップするには、.Net SDKを使用して、ポータルおよびコードから同じことを実行できます。
- ステップ1 *-AzureポータルでDocumentDBアカウントに移動します。 デモの目的で、次のスクリーンショットに示すように、さらに2つのコレクションを追加しました。
- ステップ2 *-コレクションを削除するには、そのコレクションをクリックする必要があります。 TempCollection1を選択しましょう。 次のページが表示されます。[コレクションを削除]オプションを選択します。
- ステップ3 *-確認メッセージが表示されます。 [はい]ボタンをクリックします。
TempCollection1がダッシュボードで使用できなくなっていることがわかります。
Net SDKを使用して、コードからコレクションを削除することもできます。 それを行うには、次の手順に従います。.
- ステップ1 *-削除するコレクションのIDを指定して、コレクションを削除しましょう。
リソースを削除するために必要なselfLinksを取得するための、Idによるクエリの通常のパターンです。
private async static Task DeleteCollection(DocumentClient client, string collectionId) {
Console.WriteLine();
Console.WriteLine("*** *Delete Collection {0} in {1}* ***", collectionId, database.Id);
var query = new SqlQuerySpec {
QueryText = "SELECT * FROM c WHERE c.id = @id",
Parameters = new SqlParameterCollection {
new SqlParameter {
Name = "@id", Value = collectionId
}
}
};
DocumentCollection collection = client.CreateDocumentCollectionQuery(database.SelfLink,
query).AsEnumerable().First();
await client.DeleteDocumentCollectionAsync(collection.SelfLink);
Console.WriteLine("Deleted collection {0} from database {1}", collectionId,
database.Id);
}
ここでは、パラメーター化されたクエリを作成する好ましい方法を示します。 collectionIdをハードコーディングしていないため、このメソッドを使用してコレクションを削除できます。 このSqlQuerySpecのパラメーターのプロパティに割り当てられたこのSqlParameterCollectionでIdパラメーターが定義されている場合、Idによって特定のコレクションを照会しています。
次に、SDKは、その内部にcollectionIdが埋め込まれたDocumentDBの最終クエリ文字列を構築する作業を行います。
- ステップ2 *-クエリを実行し、SelfLinkを使用してCreateDocumentClientタスクからコレクションを削除します。
private static async Task CreateDocumentClient() {
//Create a new instance of the DocumentClient
using (var client = new DocumentClient(new Uri(EndpointUrl), AuthorizationKey)) {
database = client.CreateDatabaseQuery("SELECT *FROM c WHERE c.id =
'myfirstdb'").AsEnumerable().First();
await DeleteCollection(client, "TempCollection");
}
}
以下は、Program.csファイルの完全な実装です。
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.Documents.Linq;
using Newtonsoft.Json;
namespace DocumentDBDemo {
class Program {
private const string EndpointUrl = "https://azuredocdbdemo.documents.azure.com:443/";
private const string AuthorizationKey = "BBhjI0gxdVPdDbS4diTjdloJq7Fp4L5RO/
StTt6UtEufDM78qM2CtBZWbyVwFPSJIm8AcfDu2O+AfV T+TYUnBQ==";
private static Database database;
static void Main(string[] args) {
try {
CreateDocumentClient().Wait();
} catch (Exception e) {
Exception baseException = e.GetBaseException();
Console.WriteLine("Error: {0}, Message: {1}", e.Message, baseException.Message);
}
Console.ReadKey();
}
private static async Task CreateDocumentClient() {
//Create a new instance of the DocumentClient
using (var client = new DocumentClient(new Uri(EndpointUrl), AuthorizationKey)) {
database = client.CreateDatabaseQuery("SELECT* FROM c WHERE c.id =
'myfirstdb'").AsEnumerable().First();
await DeleteCollection(client, "TempCollection");
//await CreateCollection(client, "MyCollection1");
//await CreateCollection(client, "MyCollection2", "S2");
////await CreateDatabase(client);
//GetDatabases(client);
//await DeleteDatabase(client);
//GetDatabases(client);
}
}
private async static Task CreateCollection(DocumentClient client,
string collectionId, string offerType = "S1") {
Console.WriteLine();
Console.WriteLine("*** *Create Collection {0} in {1}* ***", collectionId,
database.Id);
var collectionDefinition = new DocumentCollection { Id = collectionId };
var options = new RequestOptions { OfferType = offerType };
var result = await client.CreateDocumentCollectionAsync(database.SelfLink,
collectionDefinition, options);
var collection = result.Resource;
Console.WriteLine("Created new collection");
ViewCollection(collection);
}
private static void ViewCollection(DocumentCollection collection) {
Console.WriteLine("Collection ID: {0} ", collection.Id);
Console.WriteLine("Resource ID: {0} ", collection.ResourceId);
Console.WriteLine("Self Link: {0} ", collection.SelfLink);
Console.WriteLine("Documents Link: {0} ", collection.DocumentsLink);
Console.WriteLine("UDFs Link: {0} ", collection.UserDefinedFunctionsLink);
Console.WriteLine("StoredProcs Link: {0} ", collection.StoredProceduresLink);
Console.WriteLine("Triggers Link: {0} ", collection.TriggersLink);
Console.WriteLine("Timestamp: {0} ", collection.Timestamp);
}
private async static Task DeleteCollection(DocumentClient client,
string collectionId) {
Console.WriteLine();
Console.WriteLine("*** *Delete Collection {0} in {1}* ***", collectionId,
database.Id);
var query = new SqlQuerySpec {
QueryText = "SELECT * FROM c WHERE c.id = @id", Parameters = new
SqlParameterCollection {
new SqlParameter {
Name = "@id", Value = collectionId
}
}
};
DocumentCollection collection = client.CreateDocumentCollectionQuery
(database.SelfLink, query).AsEnumerable().First();
await client.DeleteDocumentCollectionAsync(collection.SelfLink);
Console.WriteLine("Deleted collection {0} from database {1}", collectionId,
database.Id);
}
}
}
上記のコードをコンパイルして実行すると、次の出力が表示されます。
*** *Delete Collection TempCollection in myfirstdb* ***
Deleted collection TempCollection from database myfirstdb