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

指定查询

本页内容

  • 概述
  • 示例数据
  • 精确匹配
  • 比较运算符
  • 逻辑运算符
  • 数组运算符
  • 元素运算符
  • 评估运算符
  • 更多信息
  • API 文档

在本指南中,您可以学习如何使用C驱动程序指定查询。

您可以在查询时定义一个查询过滤器,以从集合中检索特定文档。查询过滤器是一个表达式,它指定MongoDB在读取或写入操作中用于匹配文档的搜索标准。通过定义查询过滤器,您可以指示驱动程序搜索与查询完全匹配的文档,或者您可以将查询过滤器组合起来以表达更复杂的匹配标准。

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

字面值查询返回与您的查询筛选器完全匹配的文档。

以下示例将查询筛选器作为参数指定给mongoc_collection_find_with_opts()函数。该代码返回所有type字段的值为"movie"的文档。

const bson_t *doc;
bson_t *filter = BCON_NEW ("type", BCON_UTF8 ("movie"));
mongoc_cursor_t *results =
mongoc_collection_find_with_opts (collection, filter, NULL, NULL);
while (mongoc_cursor_next (results, &doc)) {
char *str = bson_as_canonical_extended_json (doc, NULL);
printf ("%s\n", str);
bson_free (str);
}
mongoc_cursor_destroy (results);
bson_destroy (filter);
{ "_id" : { "$oid" : "..." }, "title" : "Wild and Woolly", "type" : "movie", ... }
{ "_id" : { "$oid" : "..." }, "title" : "The Devil to Pay!", "type" : "movie", ... }
{ "_id" : { "$oid" : "..." }, "title" : "Traffic in Souls", "type" : "movie", ... }
{ "_id" : { "$oid" : "..." }, "title" : "Now or Never", "type" : "movie", ... }
{ "_id" : { "$oid" : "..." }, "title" : "High and Dizzy", "type" : "movie", ... }
...

比较运算符将文档字段值与查询筛选器中指定的值进行比较。以下是一些常见的比较运算符

  • $gt:大于

  • $lte:小于或等于

  • $ne:不等于

要查看比较运算符的完整列表,请参阅MongoDB服务器手册中的比较查询运算符指南。

以下示例将比较运算符作为参数指定给mongoc_collection_find_with_opts()函数中的查询筛选器。该代码返回所有year字段的值大于2015的文档。

const bson_t *doc;
bson_t *filter = BCON_NEW ("year", "{", "$gt", BCON_INT32 (2015), "}");
mongoc_cursor_t *results =
mongoc_collection_find_with_opts (collection, filter, NULL, NULL);
while (mongoc_cursor_next (results, &doc)) {
char *str = bson_as_canonical_extended_json (doc, NULL);
printf ("%s\n", str);
bson_free (str);
}
mongoc_cursor_destroy (results);
bson_destroy (filter);
{ "_id" : ..., "title" : "The Masked Saint", "year" : { "$numberInt" : "2016" }, ... }

逻辑运算符通过应用于两个或多个表达式结果的逻辑来匹配文档。以下是一个逻辑运算符列表

  • $and,它返回所有匹配所有子句条件的文档

  • $or,它返回所有匹配一个子句条件的文档

  • $nor,它返回所有不匹配任何子句条件的文档

  • $not,它返回所有不匹配表达式的文档

要了解更多关于逻辑运算符的信息,请参阅MongoDB服务器手册中的逻辑查询运算符指南。

以下示例在查询过滤器中指定一个逻辑运算符,将其作为mongoc_collection_find_with_opts()函数的参数。该代码返回所有year字段的值为1983或1985的文档。

const bson_t *doc;
bson_t *filter = BCON_NEW (
"$or", "[",
"{", "year", BCON_INT64 (1983), "}",
"{", "year", BCON_INT64 (1985), "}",
"]"
);
mongoc_cursor_t *results =
mongoc_collection_find_with_opts (collection, filter, NULL, NULL);
while (mongoc_cursor_next (results, &doc)) {
char *str = bson_as_canonical_extended_json (doc, NULL);
printf ("%s\n", str);
bson_free (str);
}
mongoc_cursor_destroy (results);
bson_destroy (filter);
{ "_id" : ..., "title" : "Amityville 3-D", "year" : { "$numberInt" : "1983" }, ... }
{ "_id" : ..., "title" : "Barefoot Gen", "year" : { "$numberInt" : "1983" }, ... }
{ "_id" : ..., "title" : "Betrayal", "year" : { "$numberInt" : "1983" }, ... }
{ "_id" : ..., "title" : "You're a Good Man, Charlie Brown", "year" : { "$numberInt" : "1985" }, ... }
{ "_id" : ..., "title" : "Yes: 9012 Live", "year" : { "$numberInt" : "1985" }, ... }
...

数组运算符根据数组字段中元素的值或数量来匹配文档。以下是可以使用的数组运算符列表

  • $all,它返回包含查询中所有元素的数组文档

  • $elemMatch,它返回如果它们的数组字段中的元素匹配查询中的所有条件,则返回文档

  • $size,它返回具有指定大小的数组的所有文档

要了解有关数组运算符的更多信息,请参阅MongoDB服务器手册中的数组查询运算符指南。

以下示例在查询过滤器中指定了一个数组运算符,将其作为mongoc_collection_find_with_opts()函数的参数。该代码返回所有文档,其中genres数组字段的值恰好包含2个元素。

const bson_t *doc;
bson_t *filter = BCON_NEW ("genres", "{", "$size", BCON_INT32 (2), "}");
mongoc_cursor_t *results =
mongoc_collection_find_with_opts (collection, filter, NULL, NULL);
while (mongoc_cursor_next (results, &doc)) {
char *str = bson_as_canonical_extended_json (doc, NULL);
printf ("%s\n", str);
bson_free (str);
}
mongoc_cursor_destroy (results);
bson_destroy (filter);
{ "_id" : ..., "genres" : [ "Comedy", "Romance" ], "title" : "The Devil to Pay!", ... }
{ "_id" : ..., "genres" : [ "Crime", "Drama" ], "title" : "Traffic in Souls", ... }
{ "_id" : ..., "genres" : [ "Comedy", "Short" ], "title" : "High and Dizzy", ... }
{ "_id" : ..., "genres" : [ "Comedy", "Short" ], "title" : "Now or Never", ... }
{ "_id" : ..., "genres" : [ "Drama", "Romance" ], "title" : "A Woman of Paris: A Drama of Fate", ... }
...

元素运算符基于字段的存在或类型来查询数据。

要了解更多关于元素运算符的信息,请参阅MongoDB服务器手册中的元素查询运算符指南。

以下示例将$exists运算符指定为查询过滤器中的参数,并将其作为mongoc_collection_find_with_opts()函数的参数。该代码返回所有具有num_mflix_comments字段的文档。

const bson_t *doc;
bson_t *filter = BCON_NEW ("num_mflix_comments", "{", "$exists", BCON_BOOL (true), "}");
mongoc_cursor_t *results =
mongoc_collection_find_with_opts (collection, filter, NULL, NULL);
while (mongoc_cursor_next (results, &doc)) {
char *str = bson_as_canonical_extended_json (doc, NULL);
printf ("%s\n", str);
bson_free (str);
}
mongoc_cursor_destroy (results);
bson_destroy (filter);
{ "_id" : ..., "num_mflix_comments" : { "$numberInt" : "0" }, "title" : "The Park Is Mine", ...}
{ "_id" : ..., "num_mflix_comments" : { "$numberInt" : "1" }, "title" : "The Good Father", ... }
{ "_id" : ..., "num_mflix_comments" : { "$numberInt" : "0" }, "title" : "Alpine Fire", ... }
{ "_id" : ..., "num_mflix_comments" : { "$numberInt" : "1" }, "title" : "Huang jia shi jie", ... }
{ "_id" : ..., "num_mflix_comments" : { "$numberInt" : "0" }, "title" : "Twenty Years Later", ... }
...

评估运算符基于对单个字段或整个集合文档的评估返回数据。

以下是一些常见的评估运算符:

  • $text,在文档上执行文本搜索

  • $regex,返回与指定正则表达式匹配的文档

  • $mod 对字段值执行 模运算,并返回余数等于指定值的文档

要查看评估运算符的完整列表,请参阅 MongoDB 服务器手册中的 评估查询运算符指南。

以下示例在查询过滤器中将评估运算符指定为 mongoc_collection_find_with_opts() 函数的参数。该代码使用正则表达式返回所有标题字段值中至少有两个连续 "p" 字符的文档。

const bson_t *doc;
bson_t *filter = BCON_NEW("title", "{", "$regex", BCON_UTF8("p{2,}"), "}");
mongoc_cursor_t *results =
mongoc_collection_find_with_opts (collection, filter, NULL, NULL);
while (mongoc_cursor_next (results, &doc)) {
char *str = bson_as_canonical_extended_json (doc, NULL);
printf ("%s\n", str);
bson_free (str);
}
mongoc_cursor_destroy (results);
bson_destroy (filter);
{ "_id" : ..., "title" : "He Who Gets Slapped", ... }
{ "_id" : ..., "title" : "David Copperfield", ... }
{ "_id" : ..., "title" : "Applause", ... }
{ "_id" : ..., "title" : "Skippy", ... }
{ "_id" : ..., "title" : "This Happy Breed", ... }
...

要了解更多关于查询文档的信息,请参阅 MongoDB 服务器手册中的 查询文档指南。

要了解更多关于使用 C 驱动程序检索文档的信息,请参阅检索数据.

要了解更多关于 mongoc_collection_find_with_opts() 函数的信息,请参阅API 文档。

返回

从 MongoDB 读取数据