DocumentDB – Partitioning

DocumentDB – Partitioning ”; Previous Next When your database starts to grow beyond 10GB, you can scale out simply by creating new collections and then spreading or partitioning your data across more and more collections. Sooner or later a single collection, which has a 10GB capacity, will not be enough to contain your database. Now 10GB may not sound like a very large number, but remember that we”re storing JSON documents, which is just plain text and you can fit a lot of plain text documents in 10GB, even when you consider the storage overhead for the indexes. Storage isn”t the only concern when it comes to scalability. The maximum throughput available on a collection is two and a half thousand request units per second that you get with an S3 collection. Hence, if you need higher throughput, then you will also need to scale out by partitioning with multiple collections. Scale out partitioning is also called horizontal partitioning. There are many approaches that can be used for partitioning data with Azure DocumentDB. Following are most common strategies − Spillover Partitioning Range Partitioning Lookup Partitioning Hash Partitioning Spillover Partitioning Spillover partitioning is the simplest strategy because there is no partition key. It”s often a good choice to start with when you”re unsure about a lot of things. You might not know if you”ll even ever need to scale out beyond a single collection or how many collections you may need to add or how fast you may need to add them. Spillover partitioning starts with a single collection and there is no partition key. The collection starts to grow and then grows some more, and then some more, until you start getting close to the 10GB limit. When you reach 90 percent capacity, you spill over to a new collection and start using it for new documents. Once your database scales out to a larger number of collections, you”ll probably want to shift to a strategy that”s based on a partition key. When you do that you”ll need to rebalance your data by moving documents to different collections based on whatever strategy you”re migrating to. Range Partitioning One of the most common strategies is range partitioning. With this approach you determine the range of values that a document”s partition key might fall in and direct the document to a collection corresponding to that range. Dates are very typically used with this strategy where you create a collection to hold documents that fall within the defined range of dates. When you define ranges that are small enough, where you”re confident that no collection will ever exceed its 10GB limit. For example, there may be a scenario where a single collection can reasonably handle documents for an entire month. It may also be the case that most users are querying for current data, which would be data for this month or perhaps last month, but users are rarely searching for much older data. So you start off in June with an S3 collection, which is the most expensive collection you can buy and delivers the best throughput you can get. In July you buy another S3 collection to store the July data and you also scale the June data down to a less-expensive S2 collection. Then in August, you get another S3 collection and scale July down to an S2 and June all the way down to an S1. It goes, month after month, where you”re always keeping the current data available for high throughput and older data is kept available at lower throughputs. As long as the query provides a partition key, only the collection that needs to be queried will get queried and not all the collections in the database like it happens with spillover partitioning. Lookup Partitioning With lookup partitioning you can define a partition map that routes documents to specific collections based on their partition key. For example, you could partition by region. Store all US documents in one collection, all European documents in another collection, and all documents from any other region in a third collection. Use this partition map and a lookup partition resolver can figure out which collection to create a document in and which collections to query, based on the partition key, which is the region property contained in each document. Hash Partitioning In hash partitioning, partitions are assigned based on the value of a hash function, allowing you to evenly distribute requests and data across a number of partitions. This is commonly used to partition data produced or consumed from a large number of distinct clients, and is useful for storing user profiles, catalog items, etc. Let’s take a look at a simple example of range partitioning using the RangePartitionResolver supplied by the .NET SDK. Step 1 − Create a new DocumentClient and we will create two collections in CreateCollections task. One will contain documents for users that have user IDs beginning with A through M and the other for user IDs N through Z. private static async Task CreateCollections(DocumentClient client) { await client.CreateDocumentCollectionAsync(“dbs/myfirstdb”, new DocumentCollection { Id = “CollectionAM” }); await client.CreateDocumentCollectionAsync(“dbs/myfirstdb”, new DocumentCollection { Id = “CollectionNZ” }); } Step 2 − Register the range resolver for the database. Step 3 − Create a new RangePartitionResolver<string>, which is the datatype of our partition key. The constructor takes two parameters, the property name of the partition key and a dictionary that is the shard map or partition map, which is just a list of the ranges and corresponding collections that we are predefining for the resolver. private static void RegisterRangeResolver(DocumentClient client) { //Note: uffff is the largest UTF8 value, so Mufff includes all strings that start with M. var resolver = new RangePartitionResolver<string>( “userId”, new Dictionary<Range<string>, string>() { { new Range<string>(“A”, “Muffff”), “dbs/myfirstdb/colls/CollectionAM” }, { new Range<string>(“N”, “Zuffff”), “dbs/myfirstdb/colls/CollectionNZ” }, }); client.PartitionResolvers[“dbs/myfirstdb”] = resolver; } It”s necessary to encode the largest possible UTF-8 value here. Or else the first range wouldn”t match on any Ms except the one

DocumentDB – Access Control

DocumentDB – Access Control ”; Previous Next DocumentDB provides the concepts to control access to DocumentDB resources. Access to DocumentDB resources is governed by a master key token or a resource token. Connections based on resource tokens can only access the resources specified by the tokens and no other resources. Resource tokens are based on user permissions. First you create one or more users, and these are defined at the database level. Then you create one or more permissions for each user, based on the resources that you want to allow each user to access. Each permission generates a resource token that allows either read-only or full access to a given resource and that can be any user resource within the database. Users are defined at the database level and permissions are defined for each user. Users and permissions apply to all collections in the database. Let’s take a look at a simple example in which we will learn how to define users and permissions to achieve granular security in DocumentDB. We will start with a new DocumentClient and query for the myfirstdb database. 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(); collection = client.CreateDocumentCollectionQuery(database.CollectionsLink, “SELECT * FROM c WHERE c.id = ”MyCollection””).AsEnumerable().First(); var alice = await CreateUser(client, “Alice”); var tom = await CreateUser(client, “Tom”); } } Following is the implementation for CreateUser. private async static Task<User> CreateUser(DocumentClient client, string userId) { Console.WriteLine(); Console.WriteLine(“**** Create User {0} in {1} ****”, userId, database.Id); var userDefinition = new User { Id = userId }; var result = await client.CreateUserAsync(database.SelfLink, userDefinition); var user = result.Resource; Console.WriteLine(“Created new user”); ViewUser(user); return user; } Step 1 − Create two users, Alice and Tom like any resource we create, we construct a definition object with the desired Id and call the create method and in this case we”re calling CreateUserAsync with the database”s SelfLink and the userDefinition. We get back the result from whose resource property we obtain the newly created user object. Now to see these two new users in the database. private static void ViewUsers(DocumentClient client) { Console.WriteLine(); Console.WriteLine(“**** View Users in {0} ****”, database.Id); var users = client.CreateUserQuery(database.UsersLink).ToList(); var i = 0; foreach (var user in users) { i++; Console.WriteLine(); Console.WriteLine(“User #{0}”, i); ViewUser(user); } Console.WriteLine(); Console.WriteLine(“Total users in database {0}: {1}”, database.Id, users.Count); } private static void ViewUser(User user) { Console.WriteLine(“User ID: {0} “, user.Id); Console.WriteLine(“Resource ID: {0} “, user.ResourceId); Console.WriteLine(“Self Link: {0} “, user.SelfLink); Console.WriteLine(“Permissions Link: {0} “, user.PermissionsLink); Console.WriteLine(“Timestamp: {0} “, user.Timestamp); } Step 2 − Call CreateUserQuery, against the database”s UsersLink to retrieve a list of all users. Then loop through them and view their properties. Now we have to create them first. So let”s say that we wanted to allow Alice read/write permissions to the MyCollection collection, but Tom can only read documents in the collection. await CreatePermission(client, alice, “Alice Collection Access”, PermissionMode.All, collection); await CreatePermission(client, tom, “Tom Collection Access”, PermissionMode.Read, collection); Step 3− Create a permission on a resource that is MyCollection collection so we need to get that resource a SelfLink. Step 4 − Then create a Permission.All on this collection for Alice and a Permission.Read on this collection for Tom. Following is the implementation for CreatePermission. private async static Task CreatePermission(DocumentClient client, User user, string permId, PermissionMode permissionMode, string resourceLink) { Console.WriteLine(); Console.WriteLine(“**** Create Permission {0} for {1} ****”, permId, user.Id); var permDefinition = new Permission { Id = permId, PermissionMode = permissionMode, ResourceLink = resourceLink }; var result = await client.CreatePermissionAsync(user.SelfLink, permDefinition); var perm = result.Resource; Console.WriteLine(“Created new permission”); ViewPermission(perm); } As you should come to expect by now, we do this by creating a definition object for the new permission, which includes an Id and a permissionMode, which is either Permission.All or Permission.Read, and the SelfLink of the resource that”s being secured by the permission. Step 5 − Call CreatePermissionAsync and get the created permission from the resource property in the result. To view the created permission, following is the implementation of ViewPermissions. private static void ViewPermissions(DocumentClient client, User user) { Console.WriteLine(); Console.WriteLine(“**** View Permissions for {0} ****”, user.Id); var perms = client.CreatePermissionQuery(user.PermissionsLink).ToList(); var i = 0; foreach (var perm in perms) { i++; Console.WriteLine(); Console.WriteLine(“Permission #{0}”, i); ViewPermission(perm); } Console.WriteLine(); Console.WriteLine(“Total permissions for {0}: {1}”, user.Id, perms.Count); } private static void ViewPermission(Permission perm) { Console.WriteLine(“Permission ID: {0} “, perm.Id); Console.WriteLine(“Resource ID: {0} “, perm.ResourceId); Console.WriteLine(“Permission Mode: {0} “, perm.PermissionMode); Console.WriteLine(“Token: {0} “, perm.Token); Console.WriteLine(“Timestamp: {0} “, perm.Timestamp); } This time, it”s a permission query against the user”s permissions link and we simply list each permission returned for the user. Let”s delete the Alice’s and Tom’s permissions. await DeletePermission(client, alice, “Alice Collection Access”); await DeletePermission(client, tom, “Tom Collection Access”); Following is the implementation for DeletePermission. private async static Task DeletePermission(DocumentClient client, User user, string permId) { Console.WriteLine(); Console.WriteLine(“**** Delete Permission {0} from {1} ****”, permId, user.Id); var query = new SqlQuerySpec { QueryText = “SELECT * FROM c WHERE c.id = @id”, Parameters = new SqlParameterCollection { new SqlParameter { Name = “@id”, Value = permId } } }; Permission perm = client.CreatePermissionQuery(user.PermissionsLink, query) .AsEnumerable().First(); await client.DeletePermissionAsync(perm.SelfLink); Console.WriteLine(“Deleted permission {0} from user {1}”, permId, user.Id); } Step 6 − To delete permissions, query by permission Id to get the SelfLink, and then using the SelfLink to delete the permission. Next, let’s delete the users themselves. Let’s delete both the users. await DeleteUser(client, “Alice”); await DeleteUser(client, “Tom”); Following is the implementation for DeleteUser. private async static Task DeleteUser(DocumentClient client, string userId) { Console.WriteLine(); Console.WriteLine(“**** Delete User {0} in {1} ****”, userId, database.Id); var query = new SqlQuerySpec { QueryText = “SELECT * FROM c WHERE c.id = @id”, Parameters = new SqlParameterCollection { new SqlParameter { Name = “@id”, Value = userId } } }; User user = client.CreateUserQuery(database.SelfLink, query).AsEnumerable().First(); await client.DeleteUserAsync(user.SelfLink); Console.WriteLine(“Deleted user {0} from database {1}”, userId, database.Id); } Step 7 − First

DocumentDB – Data Types

DocumentDB – Data Types ”; Previous Next JSON or JavaScript Object Notation is a lightweight text-based open standard designed for human-readable data interchange and also easy for machines to parse and generate. JSON is at the heart of DocumentDB. We transmit JSON over the wire, we store JSON as JSON, and we index the JSON tree allowing queries on the full JSON document. JSON format supports the following data types − S.No. Type & Description 1 Number Double-precision floating-point format in JavaScript 2 String Double-quoted Unicode with backslash escaping 3 Boolean True or false 4 Array An ordered sequence of values 5 Value It can be a string, a number, true or false, null, etc. 6 Object An unordered collection of key:value pairs 7 Whitespace It can be used between any pair of tokens 8 Null Empty Let’s take a look at a simple example DateTime type. Add birth date to the customer class. public class Customer { [JsonProperty(PropertyName = “id”)] public string Id { get; set; } // Must be nullable, unless generating unique values for new customers on client [JsonProperty(PropertyName = “name”)] public string Name { get; set; } [JsonProperty(PropertyName = “address”)] public Address Address { get; set; } [JsonProperty(PropertyName = “birthDate”)] public DateTime BirthDate { get; set; } } We can store, retrieve, and query using DateTime as shown in the following code. private async static Task CreateDocuments(DocumentClient client) { Console.WriteLine(); Console.WriteLine(“**** Create Documents ****”); Console.WriteLine(); var document3Definition = new Customer { Id = “1001”, Name = “Luke Andrew”, Address = new Address { AddressType = “Main Office”, AddressLine1 = “123 Main Street”, Location = new Location { City = “Brooklyn”, StateProvinceName = “New York” }, PostalCode = “11229”, CountryRegionName = “United States” }, BirthDate = DateTime.Parse(DateTime.Today.ToString()), }; Document document3 = await CreateDocument(client, document3Definition); Console.WriteLine(“Created document {0} from typed object”, document3.Id); Console.WriteLine(); } When the above code is compiled and executed, and the document is created, you will see that birth date is added now. **** Create Documents **** Created new document: 1001 { “id”: “1001”, “name”: “Luke Andrew”, “address”: { “addressType”: “Main Office”, “addressLine1”: “123 Main Street”, “location”: { “city”: “Brooklyn”, “stateProvinceName”: “New York” }, “postalCode”: “11229”, “countryRegionName”: “United States” }, “birthDate”: “2015-12-14T00:00:00”, “_rid”: “Ic8LAMEUVgAKAAAAAAAAAA==”, “_ts”: 1450113676, “_self”: “dbs/Ic8LAA==/colls/Ic8LAMEUVgA=/docs/Ic8LAMEUVgAKAAAAAAAAAA==/”, “_etag”: “”00002d00-0000-0000-0000-566efa8c0000″”, “_attachments”: “attachments/” } Created document 1001 from typed object Print Page Previous Next Advertisements ”;

DocumentDB – Limiting Records

DocumentDB – Limiting Records ”; Previous Next Microsoft has recently added a number of improvements on how you can query Azure DocumentDB, such as the TOP keyword to SQL grammar, which made queries run faster and consume fewer resources, increased the limits for query operators, and added support for additional LINQ operators in the .NET SDK. Let’s take a look at a simple example in which we will retrieve only the first two records. If you have a number of records and you want to retrieve only some of them, then you can use the Top keyword. In this example, we have a lot of records of earthquakes. Now we want to show the first two records only Step 1 − Go to the query explorer and run this query. SELECT * FROM c WHERE c.magnitude > 2.5 You will see that it has retrieved four records because we have not specified TOP keyword yet. Step 2 − Now use the TOP keyword with same query. Here we have specified the TOP keyword and ‘2’ means that we want two records only. SELECT TOP 2 * FROM c WHERE c.magnitude > 2.5 Step 3 − Now run this query and you will see that only two records are retrieved. Similarly, you can use TOP keyword in code using .Net SDK. Following is the implementation. private async static Task QueryDocumentsWithPaging(DocumentClient client) { Console.WriteLine(); Console.WriteLine(“**** Query Documents (paged results) ****”); Console.WriteLine(); Console.WriteLine(“Quering for all documents”); var sql = “SELECT TOP 3 * FROM c”; var query = client .CreateDocumentQuery(collection.SelfLink, sql) .AsDocumentQuery(); while (query.HasMoreResults) { var documents = await query.ExecuteNextAsync(); foreach (var document in documents) { Console.WriteLine(” PublicId: {0}; Magnitude: {1};”, document.publicid, document.magnitude); } } Console.WriteLine(); } Following is the CreateDocumentClient task in which are instantiated the DocumentClient and earthquake database. 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 = ”earthquake””).AsEnumerable().First(); collection = client.CreateDocumentCollectionQuery(database.CollectionsLink, “SELECT * FROM c WHERE c.id = ”earthquakedata””).AsEnumerable().First(); await QueryDocumentsWithPaging(client); } } When the above code is compiled and executed, you will see that only three records are retrieved. **** Query Documents (paged results) **** Quering for all documents PublicId: 2015p947400; Magnitude: 2.515176918; PublicId: 2015p947373; Magnitude: 1.506774108; PublicId: 2015p947329; Magnitude: 1.593394461; Print Page Previous Next Advertisements ”;

DocumentDB – Insert Document

DocumentDB – Insert Document ”; Previous Next In this chapter, we will get to work with actual documents in a collection. You can create documents using either Azure portal or .Net SDK. Creating Documents with the Azure Portal Let’s take a look at the following steps to add document to your collection. Step 1 − Add new collection Families of S1 pricing tier in myfirstdb. Step 2 − Select the Families collection and click on Create Document option to open the New Document blade. This is just a simple text editor that lets you type any JSON for a new document. Step 3 − As this is raw data entry, let’s enter our first document. { “id”: “AndersenFamily”, “lastName”: “Andersen”, “parents”: [ { “firstName”: “Thomas”, “relationship”: “father” }, { “firstName”: “Mary Kay”, “relationship”: “mother” } ], “children”: [ { “firstName”: “Henriette Thaulow”, “gender”: “female”, “grade”: 5, “pets”: [ { “givenName”: “Fluffy”, “type”: “Rabbit” } ] } ], “location”: { “state”: “WA”, “county”: “King”, “city”: “Seattle”}, “isRegistered”: true } When you enter the above document, you will see the following screen. Notice that we”ve supplied an id for the document. The id value is always required, and it must be unique across all other documents in the same collection. When you leave it out then DocumentDB would automatically generate one for you using a GUID or a Globally Unique Identifier. The id is always a string and it can”t be a number, date, Boolean, or another object, and it can”t be longer than 255 characters. Also notice the document”s hierarchal structure which has a few top-level properties like the required id, as well as lastName and isRegistered, but it also has nested properties. For instance, the parents property is supplied as a JSON array as denoted by the square brackets. We also have another array for children, even though there”s only one child in the array in this example. Step 4 − Click ‘Save’ button to save the document and we”ve created our first document. As you can see that pretty formatting was applied to our JSON, which breaks up every property on its own line indented with a whitespace to convey the nesting level of each property. The portal includes a Document Explorer, so let”s use that now to retrieve the document we just created. Step 5 − Choose a database and any collection within the database to view the documents in that collection. We currently have just one database named myfirstdb with one collection called Families, both of which have been preselected here in the dropdowns. By default, the Document Explorer displays an unfiltered list of documents within the collection, but you can also search for any specific document by ID or multiple documents based on a wildcard search of a partial ID. We have only one document in our collection so far, and we see its ID on the following screen, AndersonFamily. Step 6 − Click on the ID to view the document. Creating Documents with the .NET SDK As you know that documents are just another type of resource and you”ve already become familiar with how to treat resources using the SDK. The one big difference between documents and other resources is that, of course, they”re schema free. Thus there are a lot of options. Naturally, you can just work JSON object graphs or even raw strings of JSON text, but you can also use dynamic objects that lets you bind to properties at runtime without defining a class at compile time. You can also work with real C# objects, or Entities as they are called, which might be your business domain classes. Let’s start to create documents using .Net SDK. Following are the steps. Step 1 − Instantiate DocumentClient then we will query for the myfirstdb database and then query for the MyCollection collection, which we store in this private variable collection so that it”s accessible throughout the class. 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(); collection = client.CreateDocumentCollectionQuery(database.CollectionsLink, “SELECT * FROM c WHERE c.id = ”MyCollection””).AsEnumerable().First(); await CreateDocuments(client); } } Step 2 − Create some documents in CreateDocuments task. private async static Task CreateDocuments(DocumentClient client) { Console.WriteLine(); Console.WriteLine(“**** Create Documents ****”); Console.WriteLine(); dynamic document1Definition = new { name = “New Customer 1”, address = new { addressType = “Main Office”, addressLine1 = “123 Main Street”, location = new { city = “Brooklyn”, stateProvinceName = “New York” }, postalCode = “11229”, countryRegionName = “United States” }, }; Document document1 = await CreateDocument(client, document1Definition); Console.WriteLine(“Created document {0} from dynamic object”, document1.Id); Console.WriteLine(); } The first document will be generated from this dynamic object. This might look like JSON, but of course it isn”t. This is C# code and we”re creating a real .NET object, but there”s no class definition. Instead, the properties are inferred from the way the object is initialized. Notice that we haven”t supplied an Id property for this document. Now let”s have a look into CreateDocument. It looks like the same pattern we saw for creating databases and collections. private async static Task<Document> CreateDocument(DocumentClient client, object documentObject) { var result = await client.CreateDocumentAsync(collection.SelfLink, documentObject); var document = result.Resource; Console.WriteLine(“Created new document: {0}rn{1}”, document.Id, document); return result; } Step 3 − This time we call CreateDocumentAsync specifying the SelfLink of the collection we want to add the document to. We get back a response with a resource property that, in this case, represents the new document with its system-generated properties. The Document object is a defined class in the SDK that inherits from resource and so it has all the common resource properties, but it also includes the dynamic properties that define the schema-free document itself. private async static Task CreateDocuments(DocumentClient client) { Console.WriteLine(); Console.WriteLine(“**** Create Documents ****”); Console.WriteLine(); dynamic document1Definition = new { name = “New Customer 1”, address = new { addressType =

DocumentDB – Delete Document

DocumentDB – Delete Document ”; Previous Next In this chapter, we will learn how to delete a document from your DocumentDB account. Using Azure Portal, you can easily delete any document by opening the document in Document Explorer and click the ‘Delete’ option. It will display the confirmation message. Now press the Yes button and you will see that the document is no longer available in your DocumentDB account. Now when you want to delete a document using .Net SDK. Step 1 − It”s the same pattern as we”ve seen before where we”ll query first to get the SelfLinks of each new document. We don”t use SELECT * here, which would return the documents in their entirety, which we don”t need. Step 2 − Instead we”re just selecting the SelfLinks into a list and then we just call DeleteDocumentAsync for each SelfLink, one at a time, to delete the documents from the collection. private async static Task DeleteDocuments(DocumentClient client) { Console.WriteLine(); Console.WriteLine(“>>> Delete Documents <<<“); Console.WriteLine(); Console.WriteLine(“Quering for documents to be deleted”); var sql = “SELECT VALUE c._self FROM c WHERE STARTSWITH(c.name, ”New Customer”) = true”; var documentLinks = client.CreateDocumentQuery<string>(collection.SelfLink, sql).ToList(); Console.WriteLine(“Found {0} documents to be deleted”, documentLinks.Count); foreach (var documentLink in documentLinks) { await client.DeleteDocumentAsync(documentLink); } Console.WriteLine(“Deleted {0} new customer documents”, documentLinks.Count); Console.WriteLine(); } Step 3 − Now let’s call the above DeleteDocuments from the CreateDocumentClient task. 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(); collection = client.CreateDocumentCollectionQuery(database.CollectionsLink, “SELECT * FROM c WHERE c.id = ”MyCollection””).AsEnumerable().First(); await DeleteDocuments(client); } } When the above code is executed, you will receive the following output. ***** Delete Documents ***** Quering for documents to be deleted Found 2 documents to be deleted Deleted 2 new customer documents Print Page Previous Next Advertisements ”;

DocumentDB – Update Document

DocumentDB – Update Document ”; Previous Next In this chapter, we will learn how to update the documents. Using Azure portal, you can easily update document by opening the document in Document explorer and updating it in editor like a text file. Click ‘Save’ button. Now when you need to change a document using .Net SDK you can just replace it. You don”t need to delete and recreate it, which besides being tedious, would also change the resource id, which you wouldn”t want to do when you”re just modifying a document. Here are the following steps to update the document using .Net SDK. Let’s take a look at the following ReplaceDocuments task where we will query for documents where the isNew property is true, but we will get none because there aren”t any. So, let”s modify the documents we added earlier, those whose names start with New Customer. Step 1 − Add the isNew property to these documents and set its value to true. private async static Task ReplaceDocuments(DocumentClient client) { Console.WriteLine(); Console.WriteLine(“>>> Replace Documents <<<“); Console.WriteLine(); Console.WriteLine(“Quering for documents with ”isNew” flag”); var sql = “SELECT * FROM c WHERE c.isNew = true”; var documents = client.CreateDocumentQuery(collection.SelfLink, sql).ToList(); Console.WriteLine(“Documents with ”isNew” flag: {0} “, documents.Count); Console.WriteLine(); Console.WriteLine(“Quering for documents to be updated”); sql = “SELECT * FROM c WHERE STARTSWITH(c.name, ”New Customer”) = true”; documents = client.CreateDocumentQuery(collection.SelfLink, sql).ToList(); Console.WriteLine(“Found {0} documents to be updated”, documents.Count); foreach (var document in documents) { document.isNew = true; var result = await client.ReplaceDocumentAsync(document._self, document); var updatedDocument = result.Resource; Console.WriteLine(“Updated document ”isNew” flag: {0}”, updatedDocument.isNew); } Console.WriteLine(); Console.WriteLine(“Quering for documents with ”isNew” flag”); sql = “SELECT * FROM c WHERE c.isNew = true”; documents = client.CreateDocumentQuery(collection.SelfLink, sql).ToList(); Console.WriteLine(“Documents with ”isNew” flag: {0}: “, documents.Count); Console.WriteLine(); } Step 2 − Get the documents to be updated using the same STARTSWITH query and that gives us the documents, which we are getting back here as dynamic objects. Step 3 − Attach the isNew property and set it to true for each document. Step 4 − Call ReplaceDocumentAsync, passing in the document”s SelfLink, along with the updated document. Now just to prove that this worked, query for documents where isNew equaled true. Let’s call the above queries from the CreateDocumentClient task. 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(); collection = client.CreateDocumentCollectionQuery(database.CollectionsLink, “SELECT * FROM c WHERE c.id = ”MyCollection””).AsEnumerable().First(); //await CreateDocuments(client); //QueryDocumentsWithSql(client); //await QueryDocumentsWithPaging(client); //QueryDocumentsWithLinq(client); await ReplaceDocuments(client); } } When the above code is compiled and executed, you will receive the following output. **** Replace Documents **** Quering for documents with ”isNew” flag Documents with ”isNew” flag: 0 Quering for documents to be updated Found 2 documents to be updated Updated document ‘isNew’ flag: True Updated document ‘isNew’ flag: True Quering for documents with ”isNew” flag Documents with ”isNew” flag: 2 Print Page Previous Next Advertisements ”;

DocumentDB – List Databases

DocumentDB – List Databases ”; Previous Next So far, we have created two databases in our DocumentDB account, first one is created using Azure portal while the second database is created using .Net SDK. Now to view these databases, you can use Azure portal. Go to your DocumentDB account on Azure portal and you will see two databases now. You can also view or list the databases from your code using .Net SDK. Following are the steps involved. Step 1 − Issue a database Query with no parameters which returns a complete list, but you can also pass in a query to look for a specific database or specific databases. private static void GetDatabases(DocumentClient client) { Console.WriteLine(); Console.WriteLine(); Console.WriteLine(“******** Get Databases List ********”); var databases = client.CreateDatabaseQuery().ToList(); foreach (var database in databases) { Console.WriteLine(” Database Id: {0}; Rid: {1}”, database.Id, database.ResourceId); } Console.WriteLine(); Console.WriteLine(“Total databases: {0}”, databases.Count); } You will see that there are a bunch of these CreateQuery methods for locating collections, documents, users, and other resources. These methods don”t actually execute the query, they just define the query and return an iterateable object. It”s the call to ToList() that actually executes the query, iterates the results, and returns them in a list. Step 2 − Call GetDatabases method from the CreateDocumentClient task after DocumentClient is instantiated. Step 3 − You also need to comment the CreateDatabase task or change the database id, otherwise you will get an error message that the database exists. using (var client = new DocumentClient(new Uri(EndpointUrl), AuthorizationKey)) { //await CreateDatabase(client); GetDatabases(client); } Following is the complete Program.cs file so far. 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==”; 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)) { await CreateDatabase(client); GetDatabases(client); } } private async static Task CreateDatabase(DocumentClient client) { Console.WriteLine(); Console.WriteLine(“******** Create Database *******”); var databaseDefinition = new Database { Id = “mynewdb” }; var result = await client.CreateDatabaseAsync(databaseDefinition); var database = result.Resource; Console.WriteLine(” Database Id: {0}; Rid: {1}”, database.Id, database.ResourceId); Console.WriteLine(“******** Database Created *******”); } private static void GetDatabases(DocumentClient client) { Console.WriteLine(); Console.WriteLine(); Console.WriteLine(“******** Get Databases List ********”); var databases = client.CreateDatabaseQuery().ToList(); foreach (var database in databases) { Console.WriteLine(” Database Id: {0}; Rid: {1}”, database.Id, database.ResourceId); } Console.WriteLine(); Console.WriteLine(“Total databases: {0}”, databases.Count); } } } When the above code is compiled and executed you will receive the following output which contains the Database and Resources IDs of both the databases. In the end you will also see the total number of databases. ******** Get Databases List ******** Database Id: myfirstdb; Rid: Ic8LAA== Database Id: mynewdb; Rid: ltpJAA== Total databases: 2 Print Page Previous Next Advertisements ”;

DocumentDB – Home

DocumentDB Tutorial PDF Version Quick Guide Resources Job Search Discussion DocumentDB is Microsoft”s newest NoSQL document database platform that runs on Azure. DocumentDB is designed keeping in mind the requirements of managing data for latest applications. This tutorial explains the basics of DocumentDB with illustrative examples. Audience This tutorial is designed for beginners, i.e., for developers who want to get acquainted with how DocumentDB works. Prerequisites It is an elementary tutorial that explains the basics of DocumentDB and there are no prerequisites as such. However, it will certainly help if you have some prior exposure to NoSQL technologies. Print Page Previous Next Advertisements ”;

DocumentDB – Introduction

DocumentDB – Introduction ”; Previous Next In this chapter, we will briefly discuss the major concepts around NoSQL and document databases. We will also have a quick overview of DocumentDB. NoSQL Document Database DocumentDB is Microsoft”s newest NoSQL document database, so when you say NoSQL document database then, what precisely do we mean by NoSQL, and document database? SQL means Structured Query Language which is traditional query language of relational databases. SQL is often equated with relational databases. It”s really more helpful to think of a NoSQL database as a non-relational database, so NoSQL really means non-relational. There are different types of NoSQL databases which include key value stores such as − Azure Table Storage. Column-based stores like Cassandra. Graph databases like NEO4. Document databases like MongoDB and Azure DocumentDB. Azure DocumentDB Microsoft officially launched Azure DocumentDB on April 8th, 2015, and it certainly can be characterized as a typical NoSQL document database. It”s massively scalable, and it works with schema-free JSON documents. DocumentDB is a true schema-free NoSQL document database service designed for modern mobile and web applications. It also delivers consistently fast reads and writes, schema flexibility, and the ability to easily scale a database up and down on demand. It does not assume or require any schema for the JSON documents it indexes. DocumentDB automatically indexes every property in a document as soon as the document is added to the database. DocumentDB enables complex ad-hoc queries using a SQL language, and every document is instantly queryable the moment it”s created, and you can search on any property anywhere within the document hierarchy. DocumentDB – Pricing DocumentDB is billed based on the number of collections contained in a database account. Each account can have one or more databases and each database can have a virtually unlimited number of collections, although there is an initial default quota of 100. This quota can be lifted by contacting Azure support. A collection is not only a unit of scale, but also a unit of cost, so in DocumentDB you pay per collection, which has a storage capacity of up to 10 GB. At a minimum, you”ll need one S1 collection to store documents in a database that will cost roughly $25 per month, which gets billed against your Azure subscription. As your database grows in size and exceeds 10 GB, you”ll need to purchase another collection to contain the additional data. Each S1 collection will give you 250 request units per second, and if that”s not enough, then you can scale the collection up to an S2 and get a 1000 request units per second for about $50 a month. You can also turn it all the way up to an S3 and pay around $100 a month. Print Page Previous Next Advertisements ”;