对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

计算文档

本指南中,您可以了解如何获取集合中文档数量准确估计计数。

本指南中的示例使用名为 students 的集合中的以下文档:

{ "_id": 1, "name": "Jonathon Howard ", "finalGrade": 87.5 }
{ "_id": 2, "name": "Keisha Freeman", "finalGrade": 12.3 }
{ "_id": 3, "name": "Wei Zhang", "finalGrade": 99.0 }
{ "_id": 4, "name": "Juan Gonzalez", "finalGrade": 85.5 }
{ "_id": 5, "name": "Erik Trout", "finalGrade": 72.3 }
{ "_id": 6, "name": "Demarcus Smith", "finalGrade": 88.8 }

以下 Student 类对此集合中的文档进行建模:

public class Student {
public int Id { get; set; }
public string Name { get; set; }
public double FinalGrade { get; set; }
}

注意

students 集合中的文档使用驼峰命名约定。本指南中的示例使用 ConventionPack 将集合中的字段反序列化为 Pascal 语句,然后映射到 Student 类中的属性。

如需了解有关自定义序列化的更多信息,请参阅“自定义序列化”。

要统计与您的查询筛选器匹配的文档数量,请使用 CountDocuments() 方法。如果传递空查询筛选器,则该方法将返回集合中的文档总数。

以下示例计算其中的 finalGrade 值小于 80 的文档数:

var filter = Builders<Student>.Filter.Lt(s => s.FinalGrade, 80.0);
var count = _myColl.CountDocuments(filter);
Console.WriteLine("Number of documents with a final grade less than 80: " + count);

您可以通过将 CountOptions 类型作为参数传入来修改 CountDocuments() 的行为。如果不指定任何选项,驱动程序将使用默认值。

您可以在 CountOptions 对象中设置以下属性:

属性
说明

Collation

The type of language collation to use when sorting results. See the Collation section of this page for more information.
Default: null

Hint

用于扫描要计数的文档的索引。
默认:null

Limit

要计数的最大文档数。
默认:0

MaxTime

查询在服务器上运行的最长时间。
默认:null

Skip

计数前要跳过的文档数。
默认:0

提示

当你使用 CountDocuments() 返回集合中的文档总数时,MongoDB 会执行集合扫描。你可以使用提示来利用 _id 字段的内置索引,从而避免集合扫描并改进此方法的性能。仅当使用空查询参数调用 CountDocuments() 时才使用此技术。

var filter = Builders<Student>.Filter.Empty;
CountOptions opts = new CountOptions(){Hint = "_id_"};
var count = collection.CountDocuments(filter, opts);

要为操作配置排序规则,请创建 Collation 类的实例。

下表描述了 Collation 构造函数接受的参数。它还列出了相应的类属性,您可以使用这些属性读取每个设置的值。

Parameter
说明
类属性

locale

Specifies the International Components for Unicode (ICU) locale. For a list of supported locales, see Collation Locales and Default Parameters in the MongoDB Server Manual.

If you want to use simple binary comparison, use the Collation.Simple static property to return a Collation object with the locale set to "simple".
Data Type: string

Locale

caseLevel

(可选)指定是否包含大小写比较。

当此参数为 true 时,驱动程序的行为取决于 strength 参数的值:

- 如果 strengthCollationStrength.Primary,则驱动程序会比较基本字符和大小写。
- 如果 strengthCollationStrength.Secondary,则驱动程序会比较基本字符、变音符、其他次要差异和大小写。
- 如果 strength 为任何其他值,则会忽略此参数。

当此参数为 false 时,驱动程序不会在强度级别 PrimarySecondary 包含大小写比较。

数据类型boolean
默认值false

CaseLevel

caseFirst

(Optional) Specifies the sort order of case differences during tertiary level comparisons.

Data Type: CollationCaseFirst
Default: CollationCaseFirst.Off

CaseFirst

strength

(Optional) Specifies the level of comparison to perform, as defined in the ICU documentation.

Data Type: CollationStrength
Default: CollationStrength.Tertiary

Strength

numericOrdering

(可选)指定驾驶员是否将数字字符串作为数字进行比较。如果此参数为

true10210210

false102,则驾驶员将数字字符串作为数字进行比较。示例,在比较字符串1 2102"" 和和“”,则驾驶员会将这些值视为 和 ,并发现 更大。如果此参数为 或已排除,则驾驶员会将数字字符串作为字符串进行比较。示例,在比较字符串 ""

和和“”,驾驶员一次比较一个字符。因为""小于“”,则驾驶员会找到“”小于“”。有关更多信息,请参阅MongoDB Server手册中的排序规则限制。数据类型:

boolean
默认值:false

NumericOrdering

alternate

(Optional) Specifies whether the driver considers whitespace and punctuation as base characters for purposes of comparison.

Data Type: CollationAlternate
Default: CollationAlternate.NonIgnorable (spaces and punctuation are considered base characters)

Alternate

maxVariable

(Optional) Specifies which characters the driver considers ignorable when the alternate argument is CollationAlternate.Shifted.

Data Type: CollationMaxVariable
Default: CollationMaxVariable.Punctuation (the driver ignores punctuation and spaces)

MaxVariable

normalization

(Optional) Specifies whether the driver normalizes text as needed.

Most text doesn't require normalization. For more information about normalization, see the ICU documentation.

Data Type: boolean
Default: false

Normalization

backwards

(可选)指定包含变音符号的字符串是否从字符串的后部到前部排序。

数据类型boolean
默认false

Backwards

有关排序规则的更多信息,请参阅MongoDB Server手册中的排序规则页面。

要估计集合中的文档总数,请使用 EstimatedDocumentCount() 方法。

注意

EstimatedDocumentCount() 方法比 CountDocuments() 方法更高效,因为它会使用集合的元数据而不是扫描整个集合。

您可以通过将 EstimatedDocumentCountOptions 类型作为参数传入来修改 EstimatedDocumentCount() 的行为。如果不指定任何选项,驱动程序将使用默认值。

您可以在 EstimatedDocumentCountOptions 对象中设置以下属性:

属性
说明

MaxTime

查询在服务器上运行的最长时间。
默认:null

以下示例估计了 students 集合中的文档数量:

var count = _myColl.EstimatedDocumentCount();
Console.WriteLine("Estimated number of documents in the students collection: " + count);

您可以使用 Count() 构建其方法来计算聚合管道中的文档数量。

以下示例执行以下动作:

  • 指定匹配阶段,以查找 FinalGrade 值大于 80 的文档

  • 统计符合条件的文档数量

var filter = Builders<Student>
.Filter.Gt(s => s.FinalGrade, 80);
var result = _myColl.Aggregate().Match(filter).Count();
Console.WriteLine("Number of documents with a final grade more than 80: " + result.First().Count);

要了解有关提到的操作的更多信息,请参阅以下指南:

要进一步了解本指南所讨论的任何方法或类型,请参阅以下 API 文档: