- DocumentDB 教程
- DocumentDB - 首页
- DocumentDB - 简介
- DocumentDB - 优势
- DocumentDB - 环境设置
- DocumentDB - 创建账户
- DocumentDB - 连接账户
- DocumentDB - 创建数据库
- DocumentDB - 列出数据库
- DocumentDB - 删除数据库
- DocumentDB - 创建集合
- DocumentDB - 删除集合
- DocumentDB - 插入文档
- DocumentDB - 查询文档
- DocumentDB - 更新文档
- DocumentDB - 删除文档
- DocumentDB - 数据建模
- DocumentDB - 数据类型
- DocumentDB - 限制记录
- DocumentDB - 排序记录
- DocumentDB - 索引记录
- DocumentDB - 地理空间数据
- DocumentDB - 分区
- DocumentDB - 数据迁移
- DocumentDB - 访问控制
- DocumentDB - 数据可视化
- DocumentDB 有用资源
- DocumentDB - 快速指南
- DocumentDB - 有用资源
- DocumentDB - 讨论
DocumentDB - 删除集合
您可以通过门户以及使用 .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 参数在此分配给此 SqlQuerySpec 的参数属性的 SqlParameterCollection 中定义。
然后,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
广告