检索不同的字段值
概述
在本指南中,您可以了解如何使用C驱动程序从集合中检索指定字段的唯一值。
在集合中,不同的文档可能对单个字段具有不同的值。例如,在restaurant
集合中,一个文档的 borough
值为 "Manhattan"
,另一个的 borough
值为 "Queens"
。通过使用C驱动程序,您可以检索集合中多个文档中字段的全部唯一值。
示例数据
本指南中的示例使用来自 sample_restaurants
数据库的 restaurants
集合。Atlas 示例数据集。要了解如何创建免费的 MongoDB Atlas 集群并加载数据集,请参阅 Atlas 入门指南。
distinct 命令
要获取指定字段的唯一值,请调用 mongoc_collection_read_command_with_opts()
函数,并指示它使用 distinct
命令。您还必须指定要检索数据的集合和字段。
在集合中检索唯一值
以下示例检索了 restaurants
集合中 borough
字段的唯一值
bson_t reply; bson_error_t error; bson_t *command = BCON_NEW ("distinct", BCON_UTF8 ("restaurants"), "key", BCON_UTF8 ("borough")); if (!mongoc_collection_read_command_with_opts (collection, command, NULL, NULL, &reply, &error)) { fprintf (stderr, "An error occurred: %s\n", error.message); } else { char *str = bson_as_canonical_extended_json (&reply, NULL); printf ("%s\n", str); bson_free (str); } bson_destroy (&reply); bson_destroy (command);
{ "values" : [ "Bronx", "Brooklyn", "Manhattan", "Missing", "Queens", "Staten Island" ], ... }
结果显示了集合中所有文档的 borough
字段中出现的每个唯一值。尽管多个文档在 borough
字段中具有相同的值,但每个值在结果中只出现一次。
在指定文档中检索唯一值
您可以向 distinct
命令提供 查询过滤器,以找到集合中子集文档的唯一字段值。查询过滤器是一个表达式,用于指定用于匹配操作的文档的搜索条件。
有关创建查询过滤器的更多信息,请参阅指定查询.
以下示例检索了所有具有 cuisine
字段值 "Italian"
的文档中 borough
字段的唯一值
bson_t reply; bson_error_t error; bson_t *query = BCON_NEW ("cuisine", BCON_UTF8 ("Italian")); bson_t *command = BCON_NEW ("distinct", BCON_UTF8 ("restaurants"), "key", BCON_UTF8 ("borough"), "query", BCON_DOCUMENT (query)); if (!mongoc_collection_read_command_with_opts (collection, command, NULL, NULL, &reply, &error)) { fprintf (stderr, "An error occurred: %s\n", error.message); } else { char *str = bson_as_canonical_extended_json (&reply, NULL); printf ("%s\n", str); bson_free (str); } bson_destroy (&reply); bson_destroy (command); bson_destroy (query);
{ "values" : [ "Bronx", "Brooklyn", "Manhattan", "Queens", "Staten Island" ], ... }
修改唯一行为
可以通过传递选项给mongoc_collection_read_command_with_opts()
函数来修改distinct
命令。如果您没有指定任何选项,驱动程序不会自定义操作。
下表描述了一些可以用于自定义distinct
操作的选项
选项 | 描述 |
---|---|
collation | 指定排序结果时使用的语言排序类型。更多信息请参见MongoDB服务器手册中的排序。 |
comment | 指定要附加到操作上的注释。 |
要查看可用于修改distinct
操作的完整选项列表,请参阅MongoDB服务器手册中的Distinct文档。
以下示例检索所有具有borough
字段值为“Bronx”和cuisine
字段值为“Pizza”的文档的name
字段的唯一值。它还使用comment
选项向操作添加注释。
bson_t reply; bson_error_t error; bson_t *query = BCON_NEW ("borough", BCON_UTF8 ("Bronx"), "cuisine", BCON_UTF8 ("Pizza")); bson_t *command = BCON_NEW ("distinct", BCON_UTF8 ("restaurants"), "key", BCON_UTF8 ("name"), "query", BCON_DOCUMENT (query)); bson_t *opts = BCON_NEW ("comment", BCON_UTF8 ("Bronx pizza restaurants")); if (!mongoc_collection_read_command_with_opts (collection, command, NULL, opts, &reply, &error)) { fprintf (stderr, "An error occurred: %s\n", error.message); } else { char *str = bson_as_canonical_extended_json (&reply, NULL); printf ("%s\n", str); bson_free (str); } bson_destroy (&reply); bson_destroy (command); bson_destroy (query); bson_destroy (opts);
{ "values" : [ "$1.25 Pizza", "18 East Gunhill Pizza", "2 Bros", "Aenos Pizza", "Alitalia Pizza Restaurant", ... ], ... }
更多信息
要了解更多关于distinct命令的信息,请参阅MongoDB服务器手册中的Distinct页面。
API文档
要了解更多关于 mongoc_collection_read_command_with_opts()
函数的信息,请参阅 API 文档。