MongoDB聚合—计数count

  • 时间:
  • 浏览:8

博文参考:MongoDB readConcern 原理解析

    计数有以下格式:

    hint string or document   

Behavior

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

    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  string   The name of the collection to count.

返回结果中,n代表计数值为1,而且情况汇报ok为1。

See Perform a Count for an example.

Accuracy after Unexpected Shutdown

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

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

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" : 3, "ok" : 1 }

举例

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

    Field  Type   Description

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

count

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

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

Count All Documents

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

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

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

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:

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

    无论读取关注级别怎么都可以,节点上的最新数据是因为 无需反映系统中数据的最新版本。

返回结果中,n代表计数值为13,而且命令情况汇报ok值为1。

    New in version 2.6.

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

是因为 一个多多多mongod程序运行(使用Wired Tiger存储引擎)未正常关闭,计数统计经常出先是因为 会不正确。

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

     要确保单个程序还都可以 读取其被委托人的写入,请针对副本集的主数据库使用"majority"读取关注和"majority"写入关注。

计数

    count has the following fields:

    是因为 将级别设置为"majority",你时需使用WiredTiger存储引擎,而且在启动mongod实例时指定--enableMajorityReadConcern参数)(是因为 在config参数配置文档中指定replication.enableMajorityReadConcern参数)。

统计所有文档数。

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

为了外理分片集群居于你这俩情况汇报,使用db.collection.aggregate()命令中的$group法律法律依据进行文档$sum求和。累似 以下对一个多多多集合进行计数操作

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

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

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

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

注意

     统计某个集合中文档的数量。返回一个多多多含有计数和命令情况汇报的文档。

定义

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

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

累似 :

    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.

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

Count Documents That Match a Query

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:

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

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

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

    读策略,文档类型。

为了使用 "majority"读关注级别,时需使用WiredTiger存储引擎,而且在启动命令行中加入--enableMajorityReadConcern选项(是因为 在参数配置文档中加入replication.enableMajorityReadConcern)。

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 100 seconds. However, mongod instances running with non-default --syncdelay settings may have more or less frequent checkpoints.

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

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:

    count has the following form:

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

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

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

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

计数中指定索引

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

    readConcern    document    

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":

Skip Documents in Count

Override Default Read Concern

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

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 100. These documents are then piped to the $group to perform a count. The aggregation returns the following:

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

重要

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

要确保单个程序还都可以 读取其被委托人的写入,请针对副本集的主数据库使用"majority"读取关注和"majority"写入关注。

Definition

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

在分片集合中,是因为 存储孤立文档是因为 Chunk正在迁移,则会是因为 计数的结果是因为 会不正确。

    为了使用读关注级别"majority",时需指定一个多多多非空的查询条件。

    New in version 3.2.

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

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

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

意外关机后的准确性

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

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

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

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

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

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

Important

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

    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).

覆盖默认读关注

Note

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

    版本3.2新功能

    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).

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

Examples

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

Specify the Index to Use

返回结果中,n代表计数值为3,而且命令情况汇报ok值为1。

返回结果中,n代表计数值是26条,而且命令执行情况汇报ok是1。

行为

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.

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

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.

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

    计数有以下有2个字段:

The following sections provide examples of the count command.

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

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

    强制,字符类型是因为 文档类型

    为了使用读关注级别"majority",时需指定一个多多多非空的查询条件。

跳过文档计数

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