MongoDB聚合—计数count

  • 时间:
  • 浏览:2
  • 来源:彩神大发APP_神彩大发APP官方

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

    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.

    无论读取关注级别咋样,节点上的最新数据可能我不要 反映系统中数据的最新版本。

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

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

相似于:

The following sections provide examples of the count command.

定义

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

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

计数中指定索引

Note

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

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

    计数有以下格式:

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:

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

    计数有以下哪几个字段:

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

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

    count  string   The name of the collection to count.

可能有1个mongod系统进程池池(使用Wired Tiger存储引擎)未正常关闭,计数统计突然出现可能会不正确。

意外关机后的准确性

为了统计满足查询条件的文档数量,能必须使用$match土措施。

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

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

    count has the following fields:

计数

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

博文参考:MongoDB readConcern 原理解析

    readConcern    document    

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

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

返回结果中,n代表计数值为3,有过后命令具体情况ok值为1。

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

    count has the following form:

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

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

     要确保单个系统进程池能必须读取其本人的写入,请针对副本集的主数据库使用"majority"读取关注和"majority"写入关注。

count

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

Accuracy after Unexpected Shutdown

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.

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

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

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

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

See Perform a Count for an example.

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

    Field  Type   Description

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

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

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

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

Behavior

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

    为了使用读关注级别"majority",可不可以 指定有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

行为

要确保单个系统进程池能必须读取其本人的写入,请针对副本集的主数据库使用"majority"读取关注和"majority"写入关注。

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

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

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

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

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:

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

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

统计所有文档数。

注意

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

返回结果中,n代表计数值为1,有过后具体情况ok为1。

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

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

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

以下例子先使用$match管道土措施过滤出文档,有过后将结果传输给$group管道土措施来完成文档计数。

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

以下操作统计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 3 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') and skip the first 10 matching documents:

    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.

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

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

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

Definition

Examples

Count Documents That Match a Query

Count All Documents

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'):

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.

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

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

    版本3.2新功能

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

    强制,字符类型可能文档类型

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.

    New in version 2.6.

    hint string or document   

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:

为了外理分片集群趋于稳定某种具体情况,使用db.collection.aggregate()命令中的$group土措施进行文档$sum求和。相似于以下对有1个集合进行计数操作

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

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

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

返回结果中,n代表计数值为13,有过后命令具体情况ok值为1。

在分片集合中,可能存储孤立文档可能Chunk正在迁移,则会由于计数的结果可能会不正确。

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

    MongoDB在mongo shell窗口能必须使用count()和db.collection.count()进行查询。

覆盖默认读关注

Important

Specify the Index to Use

重要

    读策略,文档类型。

举例

跳过文档计数

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

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

Override Default Read Concern

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

    New in version 3.2.

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

猜你喜欢

在.NetCore中使用Myrmec检测文件真实格式

github地址:https://github.com/rocketRobin/myrmecnuget地址:https://www.nuget.org/packages/Myr

2020-03-22

目前哪些手机可以无线充电怎么无线充电

三星S10Plus华为Mate20Pro无线充电,是近一两年来在旗舰手机上经常出显的俩个 多功能,当然,目前来看,搭载无线充电功能的手机还不过多,我来推荐几款。华为Mate2

2020-03-22

阿里云CDN直播架构与双11晚会直播实战

电商直播的最佳实践猫晚流量再创记录,阿里云直播方案护航优酷21150万用户体验【云栖精选11月刊】揭秘双11肩头的互联网技术超级工程阿里云跨界商务媒体合作芒果TV看明星也用云计

2020-03-21

国美 京东 天猫 苏宁在哪个买电器便宜靠谱?

下载百度知道APP,抢鲜体验至于价格展开完整性使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。不得劲推荐为你推荐:基本差太满换一换你对这种回答的评价是

2020-03-21

2019年即将进入尾声,你的2020年最想做什么事?

3、找到俩个 多多 可不还要长期发展的行业并现在结速投资。不怎么推荐展开全部8、希望我的第7条可不还要实现。5、希望我中间的1234条都能实现。使用百度知道APP,立即抢鲜

2020-03-21