文档菜单
文档首页
/ / /
C 驱动程序
/

指定要返回的文档

本页内容

  • 概述
  • 示例数据
  • 限制
  • 排序
  • 跳过
  • 组合限制、排序和跳过
  • 更多信息
  • API文档

在本指南中,您可以通过以下操作了解如何使用读取操作返回哪些文档:

  • limit:指定查询返回的最大文档数

  • sort:指定返回文档的排序顺序

  • skip:指定返回查询结果前要跳过的文档数

本指南中的示例使用来自Atlas示例数据集restaurants集合在sample_restaurants数据库中。要了解如何创建免费的MongoDB Atlas集群并加载示例数据集,请参阅Atlas入门指南

要指定从读取操作返回的最大文档数,将 limit 选项传递给您的 mongoc_collection_find_with_opts() 函数调用。

以下示例查找所有具有 cuisine 字段值为 "Italian" 的餐厅,并将结果限制为 5 个文档

const bson_t *doc;
bson_t *filter = BCON_NEW ("cuisine", BCON_UTF8 ("Italian"));
bson_t *opts = BCON_NEW ("limit", BCON_INT64 (5));
mongoc_cursor_t *results =
mongoc_collection_find_with_opts (collection, filter, opts, NULL);
while (mongoc_cursor_next (results, &doc)) {
char *str = bson_as_canonical_extended_json (doc, NULL);
printf ("%s\n", str);
bson_free (str);
}
bson_destroy (filter);
bson_destroy (opts);
mongoc_cursor_destroy (results);
{ "_id" : { "$oid" : "..." }, "cuisine" : "Italian", "name" : "V & T Restaurant", ... }
{ "_id" : { "$oid" : "..." }, "cuisine" : "Italian", "name" : "Mimis Restaurant & Bar", ... }
{ "_id" : { "$oid" : "..." }, "cuisine" : "Italian", "name" : "Venice Restaurant", ... }
{ "_id" : { "$oid" : "..." }, "cuisine" : "Italian", "name" : "Areo Restaurant", ... }
{ "_id" : { "$oid" : "..." }, "cuisine" : "Italian", "name" : "Tre Giovani Pizza & Pasta", ... }

提示

前面的示例返回了查询返回的前五个文档,按照 自然顺序。以下部分将描述如何按照指定的排序顺序返回文档。

要按指定顺序返回文档,请使用 sort 选项。该 sort 选项接受一个排序方向作为参数。要指定排序方向,传递 1 进行升序排序或传递 -1 进行降序排序。升序排序按从低到高的顺序排序值,而降序排序按从高到低的顺序排序值。

以下示例返回所有具有 cuisine 字段值为 "Italian" 的文档,并按 name 字段的值进行升序排序

const bson_t *doc;
bson_t *filter = BCON_NEW ("cuisine", BCON_UTF8 ("Italian"));
bson_t *opts = BCON_NEW ("sort", "{", "name", BCON_INT32 (1), "}");
mongoc_cursor_t *results =
mongoc_collection_find_with_opts (collection, filter, opts, NULL);
while (mongoc_cursor_next (results, &doc)) {
char *str = bson_as_canonical_extended_json (doc, NULL);
printf ("%s\n", str);
bson_free (str);
}
bson_destroy (filter);
bson_destroy (opts);
mongoc_cursor_destroy (results);
{ "_id" : { "$oid" : "..." }, "cuisine" : "Italian", "name" : "(Lewis Drug Store) Locanda Vini E Olii", ... }
{ "_id" : { "$oid" : "..." }, "cuisine" : "Italian", "name" : "101 Restaurant And Bar", ... }
{ "_id" : { "$oid" : "..." }, "cuisine" : "Italian", "name" : "44 Sw Ristorante & Bar", ... }
{ "_id" : { "$oid" : "..." }, "cuisine" : "Italian", "name" : "900 Park", ... }
{ "_id" : { "$oid" : "..." }, "cuisine" : "Italian", "name" : "A Voce", ... }
...

要在返回查询结果之前跳过指定数量的文档,请使用 skip 选项并传入要跳过的文档数量。skip 选项会忽略查询结果中的指定数量文档,然后返回其余结果。

以下示例返回所有具有 cuisine 字段值为 "意大利菜" 的文档,并跳过前10个文档。

const bson_t *doc;
bson_t *filter = BCON_NEW ("cuisine", BCON_UTF8 ("Italian"));
bson_t *opts = BCON_NEW ("skip", BCON_INT64 (10));
mongoc_cursor_t *results =
mongoc_collection_find_with_opts (collection, filter, opts, NULL);
while (mongoc_cursor_next (results, &doc)) {
char *str = bson_as_canonical_extended_json (doc, NULL);
printf ("%s\n", str);
bson_free (str);
}
bson_destroy (filter);
bson_destroy (opts);
mongoc_cursor_destroy (results);
{ "_id" : { "$oid" : "..." }, "cuisine" : "Italian", "name" : "Trattoria Alba", ... }
{ "_id" : { "$oid" : "..." }, "cuisine" : "Italian", "name" : "Da Umberto Restaurant", ... }
{ "_id" : { "$oid" : "..." }, "cuisine" : "Italian", "name" : "La Strada Restaurant", ... }
{ "_id" : { "$oid" : "..." }, "cuisine" : "Italian", "name" : "Pasta Lovers Trattoria", ... }
{ "_id" : { "$oid" : "..." }, "cuisine" : "Italian", "name" : "Nanni Restaurant", ... }
...

您可以在单个操作中合并 limitsortskip 选项。这允许您设置返回的排序文档的最大数量,在返回之前跳过指定数量的文档。

以下示例返回具有 cuisine 字段值为 "意大利菜" 的文档。结果按字母顺序排序,跳过前10个文档,并限制结果为5个文档。

const bson_t *doc;
bson_t *filter = BCON_NEW ("cuisine", BCON_UTF8 ("Italian"));
bson_t *opts = BCON_NEW ("limit", BCON_INT64 (5),
"skip", BCON_INT64 (10),
"sort", "{", "name", BCON_INT32 (1), "}");
mongoc_cursor_t *results =
mongoc_collection_find_with_opts (collection, filter, opts, NULL);
while (mongoc_cursor_next (results, &doc)) {
char *str = bson_as_canonical_extended_json (doc, NULL);
printf ("%s\n", str);
bson_free (str);
}
bson_destroy (filter);
bson_destroy (opts);
mongoc_cursor_destroy (results);
{ "_id" : { "$oid" : "..." }, "cuisine" : "Italian", "name" : "Acqua", ... }
{ "_id" : { "$oid" : "..." }, "cuisine" : "Italian", "name" : "Acqua Restaurant", ... }
{ "_id" : { "$oid" : "..." }, "cuisine" : "Italian", "name" : "Acqua Santa", ... }
{ "_id" : { "$oid" : "..." }, "cuisine" : "Italian", "name" : "Acquista Trattoria", ... }
{ "_id" : { "$oid" : "..." }, "cuisine" : "Italian", "name" : "Acquolina Catering", ... }

注意

调用这些方法的顺序不会改变返回的文档。驱动程序会自动重新排序调用以首先执行排序和跳过操作,然后执行限制操作。

有关指定查询的更多信息,请参阅指定查询.

有关检索文档的更多信息,请参阅检索数据。

有关 mongoc_collection_find_with_opts() 函数的更多信息,请参阅API 文档。

返回

指定返回的字段