MongoDB聚合—计数count

  • 时间:
  • 浏览:1

以下计数操作是使用orders集合中的索引 { status: 1 } ,或多或少统计ord_dt字段大于Date('01/01/2012')或多或少status字段值为"D"

返回结果中,n代表计数值为13,或多或少命令具体情况ok值为1。

On a sharded cluster, count can result in an inaccurate count if orphaned documents exist or if a chunk migration is in progress.

Skip Documents in Count

    To use a read concern level of "majority", you must use the WiredTiger storage engine and start the mongod instances with the --enableMajorityReadConcern command line option (or the replication.enableMajorityReadConcern setting if using a configuration file).

The following operation returns a count of the documents in the orders collection where the value of the ord_dt field is greater than Date('01/01/2012'):

{ "n" : 1, "ok" : 1 }

以下操作统计orders集合中ord_dt字段值大于Date('01/01/2012')的文档数。

    计数有以下格式:

The following operation counts the number of all documents in the orders collection:

返回结果中,n代表计数值为3,或多或少命令具体情况ok值为1。

To override the default read concern level of "local", use the readConcern option.

    count  string   The name of the collection to count.

为了覆盖默认读关注级别"local",使用readConcern选项。

以下内容举例演示计数命令操作。

    可不都可不可以 副本集使用"protocol version 1 "才支持"majority"读策略。副本集运行"protocol version 0"不支持"majority"读策略。

以下计数操作统计orders命令中所有的文档数量。

    强制,字符类型将会文档类型

返回结果中,n代表计数值为1,或多或少具体情况ok为1。

Override Default Read Concern

    为了使用读关注级别"majority",可不都可不可以 能指定有一个 非空的查询条件。

In the result, the n, which represents the count, is 13 and the command status ok is 1:

    To use a read concern level of "majority", you must use the WiredTiger storage engine and start the mongod instances with the --enableMajorityReadConcern command line option (or the replication.enableMajorityReadConcern setting if using a configuration file).

    将会将级别设置为"majority",你可不都可不可以 能使用WiredTiger存储引擎,或多或少在启动mongod实例时指定--enableMajorityReadConcern参数)(将会在config参数配置文档中指定replication.enableMajorityReadConcern参数)。

为了补救分片集群趋于稳定或多或少具体情况,使用db.collection.aggregate()命令中的$group法律辦法 进行文档$sum求和。类似于于以下对有一个 集合进行计数操作

    无论读取关注级别如可,节点上的最新数据将会不要再反映系统中数据的最新版本。

The following operation returns a count of the documents in the orders collection where the value of the ord_dt field is greater than Date('01/01/2012') and skip the first 10 matching documents:

{ "n" : 13, "ok" : 1 }

In the result, the n, which represents the count, is 1 and the command status ok is 1:

In the result, the n, which represents the count, is 3 and the command status ok is 1:

Important

可不都可不可以 副本集使用"protocol version 1 "才支持"majority"读关注。副本集运行"protocol version 0"不支持"majority"读策略。

    计数,字符类型,计数集合的名称。

To ensure that a single thread can read its own writes, use "majority" read concern and "majority" write concern against the primary of the replica set.

Count All Documents

    To use a read concern level of "majority", you must specify a nonempty query condition.

以下操作统计orders集合中ord_dt字段大于Date('01/01/2012') 或多或少跳过前10条匹配文档的数量。

The following sections provide examples of the count command.

    可选。指定读内容。默认级别为"local"。

此精度影响仅适用于不含高 查询文档的计数操作。

Behavior

返回结果中,n代表计数值是26条,或多或少命令执行具体情况ok是1。

为了使用 "majority"读关注级别,可不都可不可以 能使用WiredTiger存储引擎,或多或少在启动命令行中加入--enableMajorityReadConcern选项(将会在参数配置文档中加入replication.enableMajorityReadConcern)。

行为

     统计某个集合中文档的数量。返回有一个 含高 计数和命令具体情况的文档。

Accuracy after Unexpected Shutdown

意外关机后的准确性

将会有一个 mongod线程池池池(使用Wired Tiger存储引擎)未正常关闭,计数统计出先将会会不正确。

To get a count of documents that match a query condition, include the $match stage as well:

    To ensure that a single thread can read its own writes, use "majority" read concern and "majority" write concern against the primary of the replica set.

Note

    Only replica sets using protocol version 1 support "majority" read concern. Replica sets running protocol version 0 do not support "majority" read concern.

{ "n" : 26, "ok" : 1 }

注意

    Optional. Specifies the read concern. The default level is "local".

    Regardless of the read concern level, the most recent data on a node may not reflect the most recent version of the data in the system.

在分片集合中,将会存储孤立文档将会Chunk正在迁移,则会意味着计数的结果将会会不正确。

In the aggregation pipeline, $match selects the documents where either the score is greater than 70 and less than 90 or the views is greater than or equal to 60 0. These documents are then piped to the $group to perform a count. The aggregation returns the following:

重要

统计所有文档数。

The amount of drift depends on the number of insert, update, or delete operations performed between the last checkpoint and the unclean shutdown. Checkpoints usually occur every 60 seconds. However, mongod instances running with non-default --syncdelay settings may have more or less frequent checkpoints.

计数

    skip integer     Optional. The number of matching documents to skip before returning results.

In the result, the n, which represents the count, is 26, and the command status ok is 1:

在聚合管道中,$match查询出满足条件的文档,即分数大于79,且小于90的文档;将会阅读量大于等于60 0的文档。查询出来的文档传给$group法律辦法 完成计数。聚合返回的结果如下:

    New in version 3.2.

    To use the readConcern level of "majority", you must specify a nonempty query condition.

Definition

     要确保单个线程池池可可不都可不可以 读取其或多或少人的写入,请针对副本集的主数据库使用"majority"读取关注和"majority"写入关注。

The following operation on a replica set specifies a Read Concern of "majority" to read the most recent copy of the data confirmed as having been written to a majority of the nodes.

覆盖默认读关注

在副本集上指定读关注级别为"majority",为了读取当前将会同步到所有节点的数据。

    可选。强制使用索引。将会使用字符类型则指定索引名称,将会使用文档类型则指定索引文档。

    MongoDB also provides the count() and db.collection.count() wrapper methods in the mongo shell.

    Field  Type   Description

为了统计满足查询条件的文档数量,可可不都可不可以 使用$match法律辦法 。

定义

类似于于:

博文参考:MongoDB readConcern 原理解析

Run validate on each collection on the mongod to to restore the correct statistics after an unclean shutdown.

    限制,整数,可选。返回匹配文档的最大行数。

    New in version 2.6.

The following example selects documents to process using the $match pipeline operator and then pipes the results to the $group pipeline operator to compute a count of the documents:

    版本3.2新功能

计数中指定索引

To avoid these situations, on a sharded cluster, use the $group stage of the db.collection.aggregate() method to $sum the documents. For example, the following operation counts the documents in a collection:

After an unclean shutdown of a mongod using the Wired Tiger storage engine, count statistics reported by count may be inaccurate.

Examples

    为了使用读关注级别"majority",可不都可不可以 能指定有一个 非空的查询条件。

     跳跃,整数,可选。在返回结果集前,跳过匹配文档的行数。

偏差值取决于在最后有一个 检查点和不正常关闭之间执行的插入,更新或删除操作的数量。 检查点通常每60 秒趋于稳定一次。 或多或少,使用非默认--syncdelay设置运行的mongod实例将会有更多或更少的频繁检查点。

count

    query  document    Optional. A query that selects which documents to count in a collection.

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

下面执行一下计数的例子。

对于符合查询条件的文档计数

要确保单个线程池池可可不都可不可以 读取其或多或少人的写入,请针对副本集的主数据库使用"majority"读取关注和"majority"写入关注。

举例

    limit     integer Optional. The maximum number of matching documents to return.

    MongoDB在mongo shell窗口可可不都可不可以 使用count()和db.collection.count()进行查询。

The following operation uses the index { status: 1 } to return a count of the documents in the orders collection where the value of the ord_dt field is greater than Date('01/01/2012') and the status field is equal to "D":

    Optional. The index to use. Specify either the index name as a string or the index specification document.

{ "n" : 3, "ok" : 1 }

This loss of accuracy only applies to count operations that do not include a query document.

{ "_id" : null, "count" : 5 }

     类似于于:{ "shards" : { "s1" : 5 }, "n" : 5, "ok" : 1 }

    readConcern    document    

    hint string or document   

Specify the Index to Use

    count has the following fields:

     查询,文档类型,可选。查询集合中哪此要计数的文档。

Count Documents That Match a Query

See Perform a Count for an example.

返回结果:{ "shards" : { "s1" : 11 }, "n" : 10, "ok" : 1 }

跳过文档计数

    Counts the number of documents in a collection. Returns a document that contains this count and as well as the command status.

    读策略,文档类型。

以下例子先使用$match管道法律辦法 过滤出文档,或多或少将结果传输给$group管道法律辦法 来完成文档计数。

    计数有以下十十几个 字段:

    Only replica sets using protocol version 1 support "majority" read concern. Replica sets running protocol version 0 do not support "majority" read concern.

    count has the following form: