DocumentDB - Usuń kolekcję
Aby usunąć kolekcję lub kolekcje, możesz zrobić to samo z portalu, a także z kodu za pomocą .Net SDK.
Step 1- Przejdź do swojego konta DocumentDB w witrynie Azure Portal. Do celów demonstracyjnych dodałem jeszcze dwie kolekcje, jak widać na poniższym zrzucie ekranu.

Step 2- Aby usunąć dowolną kolekcję, musisz ją kliknąć. Wybierzmy TempCollection1. Zobaczysz następującą stronę, wybierz opcję „Usuń kolekcję”.

Step 3- Wyświetli komunikat potwierdzający. Teraz kliknij przycisk „Tak”.

Zobaczysz, że kolekcja TempCollection1 nie jest już dostępna na pulpicie nawigacyjnym.

Możesz także usunąć kolekcje ze swojego kodu za pomocą .Net SDK. Aby to zrobić, wykonaj następujące kroki.
Step 1 - Usuńmy kolekcję, podając identyfikator kolekcji, którą chcemy usunąć.
Jest to typowy wzorzec odpytywania przez Id w celu uzyskania selfLinks potrzebnych do usunięcia zasobu.
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);
}
Tutaj widzimy preferowany sposób konstruowania sparametryzowanego zapytania. Nie kodujemy na stałe elementu collectionId, więc ta metoda może służyć do usuwania dowolnej kolekcji. Pytamy o określoną kolekcję według Id, w której parametr Id jest zdefiniowany w tym SqlParameterCollection przypisanym do właściwości parametru tego SqlQuerySpec.
Następnie zestaw SDK wykonuje pracę polegającą na konstruowaniu końcowego ciągu zapytania dla bazy danych DocumentDB z osadzonym w nim elementem collectionId.
Step 2 - Uruchom zapytanie, a następnie użyj jego SelfLink, aby usunąć kolekcję z zadania 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");
}
}
Poniżej znajduje się pełna implementacja pliku 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);
}
}
}
Gdy powyższy kod zostanie skompilowany i wykonany, otrzymasz następujące dane wyjściowe.
**** Delete Collection TempCollection in myfirstdb ****
Deleted collection TempCollection from database myfirstdb