Microsoft has these days delivered a number of improvements on how you can query Azure DocumentDB, together with the TOP keyword to SQL grammar, which made queries run faster and consume fewer assets, multiplied the boundaries for question operators, and added aid for additional LINQ operators in the .NET SDK.
Let’s take a look at a easy instance wherein we are able to retrieve only the first two records. If you have a number of records and also you need to retrieve only some of them, then you may use the Top keyword. In this example, we have a number of records of earthquakes.
Now we want to reveal the primary records best
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 4 information due to the fact we've now not targeted TOP keyword yet.
Step 2 − Now use the TOP key-word with same query. Here we've distinct the TOP key-word and ‘2’ means that we need two information best.
SELECT TOP 2 * FROM c
WHERE c.magnitude > 2.5
Step 3 − Now run this question and you will see that most effective statistics are retrieved.
Similarly, you can use TOP key-word in code the use of .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 mission in that 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 performed, you will see that best three statistics 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;
