Mongodb 返回错误计数
Mongodb returning wrong count
我从几 GB 的数据文件中流式传输数据并导入到 Mongo 集合中。该文件已压缩,所以我做了 zcat file.gz | mongoimport
我知道我应该在集合中包含 454229 个文档。它是空的,文件有 454229 条记录。完成后,我尝试计数并得到 459680。我清空集合并重复该过程。同样的结果。然后我在“_id”列上尝试了一个不同的,尝试了长度,并得到了 454229。为了确保没有其他进程正在更改数据,我尝试了两次计数和不同,分别是不同的,然后是计数,然后是计数,然后是不同的。
rs1:PRIMARY> db.amazon_xml_data.distinct("_id").length;
454229
rs1:PRIMARY> db.amazon_xml_data.count({});
459680
rs1:PRIMARY> db.amazon_xml_data.count({});
459680
rs1:PRIMARY> db.amazon_xml_data.distinct("_id").length;
454229
Mongo 版本为 3.2.22。我在日志文件的末尾也有这段文字...
amazon_xml_data 1.11GB
2020-02-15T14:37:07.302+0530 imported 454229 documents
所以我几乎可以肯定count返回的数字是错误的。
在 2 种情况下 count
可能不准确:
After an unclean shutdown of a mongod using the Wired Tiger storage engine, count statistics reported by count may be inaccurate.
Run validate on each collection on the mongod to restore the correct statistics after an unclean shutdown.
https://docs.mongodb.com/v3.2/reference/command/count/#accuracy-after-unexpected-shutdown
On a sharded cluster, db.collection.count()
can result in an inaccurate count if orphaned documents exist or if a chunk migration is in progress.
https://docs.mongodb.com/v3.2/reference/method/db.collection.count/#sharded-clusters
更好用:
db.amazon_xml_data.aggregate(
[
{ $group: { _id: null, count: { $sum: 1 } } }
]
)
我从几 GB 的数据文件中流式传输数据并导入到 Mongo 集合中。该文件已压缩,所以我做了 zcat file.gz | mongoimport
我知道我应该在集合中包含 454229 个文档。它是空的,文件有 454229 条记录。完成后,我尝试计数并得到 459680。我清空集合并重复该过程。同样的结果。然后我在“_id”列上尝试了一个不同的,尝试了长度,并得到了 454229。为了确保没有其他进程正在更改数据,我尝试了两次计数和不同,分别是不同的,然后是计数,然后是计数,然后是不同的。
rs1:PRIMARY> db.amazon_xml_data.distinct("_id").length;
454229
rs1:PRIMARY> db.amazon_xml_data.count({});
459680
rs1:PRIMARY> db.amazon_xml_data.count({});
459680
rs1:PRIMARY> db.amazon_xml_data.distinct("_id").length;
454229
Mongo 版本为 3.2.22。我在日志文件的末尾也有这段文字...
amazon_xml_data 1.11GB
2020-02-15T14:37:07.302+0530 imported 454229 documents
所以我几乎可以肯定count返回的数字是错误的。
在 2 种情况下 count
可能不准确:
After an unclean shutdown of a mongod using the Wired Tiger storage engine, count statistics reported by count may be inaccurate. Run validate on each collection on the mongod to restore the correct statistics after an unclean shutdown.
https://docs.mongodb.com/v3.2/reference/command/count/#accuracy-after-unexpected-shutdown
On a sharded cluster,
db.collection.count()
can result in an inaccurate count if orphaned documents exist or if a chunk migration is in progress.
https://docs.mongodb.com/v3.2/reference/method/db.collection.count/#sharded-clusters
更好用:
db.amazon_xml_data.aggregate(
[
{ $group: { _id: null, count: { $sum: 1 } } }
]
)