MongoDB 中遇到的坑

MongoDB 中遇到的坑

Mongodb count

  • 在项目中遇到一个这样的问题, 一个集合通过find查询的出来的结果是136, 而通过db.collection.find().count()之后的结果是79

  • 本以为是工具的问题,但通过去mongo shell 直接去查询得到一样的结果

  • 之后查阅了一番资料,终于在mongo的官方文档上找到了答案,得到答案很让我吃惊,竟然是建议我不要在不添加查询的条件的基础上进行count操作

  • 原文如下:

  • Avoid using the db.collection.count() method without a query predicate since without the query predicate, the method returns results based on the collection’s metadata, which may result in an approximate count. In particular,

    • On a sharded cluster, the resulting count will not correctly filter out orphaned documents.
    • After an unclean shutdown, the count may be incorrect.
  • For counts based on collection metadata, see also collStats pipeline stage with the count option.

  • 通过这个可以看出,我这次遇到很可能是因为一次意外的关闭,导致了一些数据没有被记录上,从而导致count数据错误

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值