mongo:返回不等于count() [英] mongo:the return don't equal count()
本文介绍了mongo:返回不等于count()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个问题困扰我很长时间,它遵循(例如代码演示):
I have a problem that's been troubling me for a long time and it follows (code demo for example):
from mongoengine import*
Class Scan(Documnet):
name=StringField()
.....
queryset=Scan.objects.filter(name="Bob")
number1=queryset.count()
number2=len(queryset)
但是, number1 = 1782
和 number2 = 1668
, number1!= number2
任何人都可以告诉我原因?
Anyone can tell me the reason?
推荐答案
这是由于文档
为了避免这些情况,在分片的群集上,使用 db的$ group阶段。 collection.aggregate()
方法来$文件。例如,以下操作计算集合中的文档:
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:
您可以使用 聚合
方法来做到这一点在文档中建议。
You can use the aggregate
method to do this as suggested in the documentation.
Scan.aggregate(
{'$group': {
'_id': None,
'count': {'$sum': 1}
}}
)
这篇关于mongo:返回不等于count()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文