Documentdb 简明教程
DocumentDB - Limiting Records
Microsoft 最近添加了许多改善,说明您如何查询 Azure DocumentDB,例如 SQL 语法的 TOP 关键字,它使查询运行更快并消耗更少的资源,增加了查询运算符的限制,并且为 .NET SDK 中的其他 LINQ 运算符添加了支持。
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.
让我们来看一个简单的示例,我们将在其中仅检索前两条记录。如果您有一些记录,并且您想仅检索其中某些记录,那么您可以使用 Top 关键字。在此示例中,我们有许多地震记录。
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 - 转到查询浏览器并运行此查询。
Step 1 − Go to the query explorer and run this query.
SELECT * FROM c
WHERE c.magnitude > 2.5
您将会看到它已检索了 4 条记录,因为我们尚未指定 TOP 关键字。
You will see that it has retrieved four records because we have not specified TOP keyword yet.

Step 2 - 现在与相同查询一起使用 TOP 关键字。这里我们指定了 TOP 关键字,而“2”表示我们仅需要两条记录。
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 - 现在运行此查询,您将会看到仅检索了两条记录。
Step 3 − Now run this query and you will see that only two records are retrieved.

同样地,您可以使用 .Net SDK 的代码中的 TOP 关键字。以下是实现。
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();
}
以下是 CreateDocumentClient 任务,在其中实例化了 DocumentClient 和地震数据库。
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);
}
}
当上述代码编译并执行时,您将会看到仅检索了 3 条记录。
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;