2017-01-19 66 views
1

我有一個mongo集合。當我運行時

> db.coll.count() 
6570 
>> db.coll.aggregate({ "$group" : { "_id": null, "total": { $sum : 1 } }}) 
{ "_id" : null, "total" : 6575 } 

爲什麼結果不一樣?

+0

可能重複的[mongo:返回不等於count()](http://stackoverflow.com/questions/39698862/mongothe-return-dont-equal-count) – styvane

+0

另請參閱:[計數之間的差異()和find()。count()在MongoDB中](http://stackoverflow.com/questions/32666330/difference-between-count-and-find-count-in-mongodb) – styvane

回答

-2

你爲什麼不挖一個深一點找到不一致

db.coll.find({},{_id:1}).toArray().length; 

經文

db.coll.aggregate({ "$group" : { "_id": null, "ids": { $push : $_id }}},{$project : {_id:0, count : {$size:"$ids"}}) 

這樣,你可以看看是否有一些額外的_ids。如果兩者在ID方面是一致的。