Docs 菜单
Docs 主页
/ / /
C 驱动程序
/

指定要返回的文档

在此页面上

  • Overview
  • 样本数据
  • Limit
  • Sort
  • 跳过
  • 组合限制、排序和跳过
  • 更多信息
  • API 文档

在本指南中,您可以学习;了解如何使用以下操作指定从读取操作中返回哪些文档:

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

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

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

本指南中的示例使用 Atlas示例数据集sample_restaurants数据库中的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字段值为"Italian"的所有文档并跳过前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字段值为"Italian"的文档。 结果按字母顺序排序,跳过前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文档。

后退

指定要返回的字段