要删除集合或集合,您可以使用.Net SDK从门户网站和代码执行相同操作.
步骤1 : 转到Azure门户上的DocumentDB帐户.出于演示的目的,我添加了两个集合,如以下屏幕截图所示.
第2步 : 要删除任何集合,您需要单击该集合.我们选择TempCollection1.您将看到以下页面,选择"删除集合"选项.
第3步 : 它将显示确认消息.现在点击"是"按钮.
你将看到仪表板上的TempCollection1不再可用.
您还可以使用.Net SDK从代码中删除集合.为此,请执行以下步骤:
步骤1 : 让我们通过指定要删除的集合的ID来删除集合.
这是Id通常用于获取删除资源所需的selfLinks的模式.
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进行硬编码,因此可以使用此方法删除任何集合.我们通过Id查询特定集合,其中Id参数在此SqlParameterCollection中定义,该参数分配给此SqlQuerySpec的参数属性.
然后SDK完成构建最终查询字符串的工作对于DocumentDB,其中嵌入了collectionId.
第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