# db.collection.estimatedDocumentCount()

在本页面

* [定义](#definition)
* [行为](#behavior)
* [例子](#examples)

## 定义

`db.collection.estimatedDocumentCount`（options）

*版本4.0.3中的新功能。*

返回集合或视图中所有文档的计数。该方法包装`count`命令。

```
db.collection.estimatedDocumentCount( <options> )
```

| 参数        | 类型       | 描述               |
| --------- | -------- | ---------------- |
| `options` | document | 可选的。影响计数行为的其他选项。 |

该`options`文档可以包含以下内容：

| 字段          | 类型      | 描述               |
| ----------- | ------- | ---------------- |
| `maxTimeMS` | integer | 可选的。允许计数运行的最长时间。 |

## 行为

### 结构

`db.collection.estimatedDocumentCount()`不使用查询过滤器，而是使用元数据返回集合的计数。

### 分片集群

在分片群集上，结果计数将无法正确过滤出 `orphaned document`。

### 不正常关机

不正常关机后，计数可能不正确。

`mongod`使用Wired Tiger存储引擎不正常关闭后，所报告的计数统计信息 `db.collection.estimatedDocumentCount()`可能不准确。

偏移量取决于在最后一个`checkpoint`与异常关闭之间执行的插入，更新或删除操作的数量。检查点通常每60秒出现一次。但是，`mongod`使用非默认`--syncdelay`设置运行的实例可能具有或多或少的频繁检查点。

`validate`在`mongod`异常关闭后，对上的每个集合运行以恢复正确的统计信息。

### 客户端断开

从MongoDB 4.2开始，如果发出`db.collection.estimatedDocumentCount()`断开连接的客户端在操作完成之前断开连接，则MongoDB将标记为终止`db.collection.estimatedDocumentCount()`（即在操作上`killOp`）。

## 例子

以下示例用于 `db.collection.estimatedDocumentCount`检索`orders`集合中所有文档的计数：

```
db.orders.estimatedDocumentCount({})
```

> 也可以看看
>
> * `db.collection.countDocuments()`
> * `count`
> * 带有count选项的collStats pipeline stage。

译者：李冠飞

校对：


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.mongoing.com/can-kao/mongo-shell-methods/collection-methods/db-collection-estimateddocumentcount.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
