MongoDB db.collection.count()与db.collection.find().length() [英] MongoDB db.collection.count() vs db.collection.find().length()

查看:289
本文介绍了MongoDB db.collection.count()与db.collection.find().length()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解为什么这些命令在mongos实例中针对同一个MongoDB集合运行时返回不同的数字?

  • db.users.count()
  • db.users.find().length()

可能是什么原因,这可能是潜在问题的征兆吗?

解决方案

我相信您的收藏集已被分片.

大多数分片数据库解决方案都存在这种差异,这是因为某些命令会考虑整个集合,即所有分片的所有文档,而另一些命令仅考虑与其连接的分片的文档.

>

这是始终要记住的事情.它主要适用于以下命令:

  • 计数
  • 返回给定字段中具有最低价值的文档
  • 返回给定字段具有最大价值的文档
  • ...

在Mongo上找到文档:

count()等效于db.collection.find(query).count() 构造. ...分片集群

在分片群集上,db.collection.count()可能会导致 如果存在孤立文档或大块迁移,则计数不正确 正在处理. ...

因此,对于Mongo,这仅仅是因为Mongo总是在后台进程中对分片中的文档进行一些重新平衡,以使分片分发符合集合中定义的分片策略. /p>

请记住,为了提供最佳性能,大多数分片解决方案都会在客户端连接到的分片上写入文档,然后再将其实际放置在原处.

这就是为什么nosql数据库经常被标记为最终一致的原因.

I would like to understand why these commands, when run from a mongos instance against the same MongoDB collection, return different numbers?

  • db.users.count()
  • db.users.find().length()

What can be the reason and can it be a sign of underlying issues?

解决方案

I believe your collection is sharded.

Most sharded databases solutions have such discrepancy, due to the fact that some commands consider the entire collection, meaning all the documents of all the shards, while some other commands only consider the documents of the shard it is connected to.

This is something to always keep in mind. It mostly applies to commands which:

  • count
  • return the document having the lowest value for a given field
  • return the document having the biggest value for a given field
  • ...

Found on Mongo docs:

count() is equivalent to the db.collection.find(query).count() construct. ... Sharded Clusters

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

So in the case of Mongo, it is simply because Mongo always runs, in a background process, some rebalancing of the documents within a shard, in order to keep the shards distribution compliant with the sharding policy defined on the collection.

Keep in mind that to offer the best performance, most sharded solutions will write the documents on the shard the client is connected to, and then later put it where it is really meant to be.

This is why nosql DBs are often flagged as eventually consistent.

这篇关于MongoDB db.collection.count()与db.collection.find().length()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆