In DocumentDB, we surely use SQL to query for files, so this bankruptcy is all approximately querying the use of the unique SQL syntax in DocumentDB. Although in case you are doing .NET development, there may be also a LINQ company that may be used and which could generate suitable SQL from a LINQ query.
Querying Document the use of Portal
The Azure portal has a Query Explorer that lets you run any SQL question against your DocumentDB database.
We will use the Query Explorer to illustrate the various distinctive capabilities and functions of the query language beginning with the most effective feasible query.
Step 1 − In the database blade, click to open the Query Explorer blade.
Remember that queries run inside the scope of a set, and so the Query Explorer lets you select the collection on this dropdown.
Step 2 − Select Families collection that is created earlier using the portal.
The Query Explorer opens up with this easy query SELECT * FROM c, which sincerely retrieves all files from the collection.
Step 3 − Execute this question by clicking the ‘Run question’ button. Then you may see that the entire file is retrieved in the Results blade.
Querying Document using .Net SDK
Following are the stairs to run some file queries the use of .Net SDK.
In this case, we want to question for the newly created files that we simply added.
Step 1 − Call CreateDocumentQuery, passing within the collection to run the query in opposition to by means of its SelfLink and the question textual content.
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 * 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(" Id: {0}; Name: {1};", document.id, document.name);
}
}
Console.WriteLine();
}
This query is also returning all files within the entire series, but we are now not calling .ToList on CreateDocumentQuery as before, which might problem as many requests as necessary to drag down all of the results in one line of code.
Step 2 − Instead, name AsDocumentQuery and this approach returns a query item with a HasMoreResults belongings.
Step 3− If HasMoreResults is real, then name ExecuteNextAsync to get the following chunk after which unload all the contents of that bite.
Step 4− You also can query using LINQ as opposed to SQL in case you decide on. Here we've described a LINQ question in q, but it won't execute until we run .ToList on it.
private static void QueryDocumentsWithLinq(DocumentClient client) {
Console.WriteLine();
Console.WriteLine("**** Query Documents (LINQ) ****");
Console.WriteLine();
Console.WriteLine("Quering for US customers (LINQ)");
var q =
from d in client.CreateDocumentQuery<Customer>(collection.DocumentsLink)
where d.Address.CountryRegionName == " United States"
select new {
Id = d.Id,
Name = d.Name,
City = d.Address.Location.City
};
var documents = q.ToList();
Console.WriteLine("Found {0} UK customers", documents.Count);
foreach (var document in documents) {
var d = document as dynamic;
Console.WriteLine(" Id: {0}; Name: {1}; City: {2}", d.Id, d.Name, d.City);
}
Console.WriteLine();
}
The SDK will convert our LINQ query into SQL syntax for DocumentDB, producing a SELECT and WHERE clause based totally on our LINQ syntax
Step 5 − Now call the above queries from the CreateDocumentClient project.
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);
await QueryDocumentsWithPaging(client);
QueryDocumentsWithLinq(client);
}
}
When the above code is accomplished, you will acquire the following output.
**** Query Documents (paged results) ****
Quering for all documents
Id: 7e9ad4fa-c432-4d1a-b120-58fd7113609f; Name: New Customer 1;
Id: 34e9873a-94c8-4720-9146-d63fb7840fad; Name: New Customer 1;
**** Query Documents (LINQ) ****
Quering for US customers (LINQ)
Found 2 UK customers
Id: 7e9ad4fa-c432-4d1a-b120-58fd7113609f; Name: New Customer 1; City: Brooklyn
Id: 34e9873a-94c8-4720-9146-d63fb7840fad; Name: New Customer 1; City: Brooklyn