In this chapter, we are able to get to work with actual files in a collection. You can create files using both Azure portal or .Net SDK.
Creating Documents with the Azure Portal
Let’s check the subsequent steps to add file on your series.
Step 1 − Add new series Families of S1 pricing tier in myfirstdb.
Step 2 − Select the Families series and click on Create Document option to open the New Document blade.
This is just a easy text editor that helps you to type any JSON for a new document.
Step 3 − As that is raw information access, allow’s input 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 report, you'll see the subsequent display.
Notice that we've got supplied an identification for the record. The identification cost is constantly required, and it must be specific across all different files within the same collection. When you go away it out then DocumentDB might mechanically generate one for you using a GUID or a Globally Unique Identifier.
The identity is constantly a string and it can not be more than a few, date, Boolean, or every other object, and it can not be longer than 255 characters.
Also note the record's hierarchal shape which has some pinnacle-level homes just like the required identification, in addition to lastName and isRegistered, however it additionally has nested homes.
For instance, the mother and father property is supplied as a JSON array as denoted through the square brackets. We also have another array for kids, even though there is only one toddler inside the array in this case.
Step 4− Click ‘Save’ button to keep the report and we've got created our first report.
As you could see that quite formatting became applied to our JSON, which breaks up each assets on its very own line indented with a whitespace to bring the nesting level of each belongings.
The portal consists of a Document Explorer, so permit's use that now to retrieve the file we simply created.
Step 5− Choose a database and any collection in the database to view the files in that collection. We presently have just one database named myfirstdb with one series known as Families, both of that have been preselected right here inside the dropdowns.
By default, the Document Explorer displays an unfiltered listing of files inside the series, but you can additionally look for any specific record by using ID or more than one documents based on a wildcard search of a partial ID.
We have most effective one document in our collection thus far, and we see its ID on the subsequent screen, AndersonFamily.
Step 6 − Click on the ID to view the file.
Creating Documents with the .NET SDK
As you understand that files are just any other form of aid and you've already turn out to be familiar with the way to treat assets the usage of the SDK.
- The one big distinction among documents and different sources is that, of course, they're schema unfastened.
- Thus there are quite a few alternatives. Naturally, you may just work JSON item graphs or even raw strings of JSON textual content, but you may additionally use dynamic items that lets you bind to residences at runtime without defining a category at bring together time.
- You also can paintings with actual C# gadgets, or Entities as they may be called, which might be your business area training.
Let’s start to create documents using .Net SDK. Following are the stairs.
Step 1 − Instantiate DocumentClient then we are able to query for the myfirstdb database after which query for the MyCollection collection, which we keep in this personal variable collection so that it's available in the course of the elegance.
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 mission.
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 report could be generated from this dynamic object. This would possibly appear like JSON, but of course it is not. This is C# code and we are creating a real .NET object, but there is no elegance definition. Instead, the homes are inferred from the way the object is initialized.
Notice that we haven't provided an Id belongings for this record.
Now permit's have a investigate CreateDocument. It looks as if the equal pattern we noticed for growing 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}\r\n{1}", document.Id, document);
return result;
}
Step 3 − This time we call CreateDocumentAsync specifying the SelfLink of the collection we want to add the report to. We get lower back a response with a useful resource assets that, in this example, represents the brand new document with its gadget-generated properties.
The Document item is a defined elegance within the SDK that inherits from aid and so it has all the common useful resource houses, but it is usually the dynamic properties that outline the schema-loose report 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 = "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();
}
When the above code is compiled and done you will receive the subsequent output.
**** Create Documents ****
Created new document: 34e9873a-94c8-4720-9146-d63fb7840fad {
"name": "New Customer 1",
"address": {
"addressType": "Main Office",
"addressLine1": "123 Main Street",
"location": {
"city": "Brooklyn", "stateProvinceName": "New York"
},
"postalCode": "11229", "countryRegionName": "United States"
},
"id": "34e9873a-94c8-4720-9146-d63fb7840fad",
"_rid": "Ic8LAMEUVgACAAAAAAAAAA==",
"_ts": 1449812756,
"_self": "dbs/Ic8LAA==/colls/Ic8LAMEUVgA=/docs/Ic8LAMEUVgACAAAAAAAAAA==/",
"_etag": "\"00001000-0000-0000-0000-566a63140000\"",
"_attachments": "attachments/"
}
Created document 34e9873a-94c8-4720-9146-d63fb7840fad from dynamic object
Created record 34e9873a-94c8-4720-9146-d63fb7840fad from dynamic object
As you may see, we haven’t furnished an Id, but DocumentDB generated this one for us for the brand new document.