2017-07-17 49 views
0

我收藏〜2000000個文件。 字段unit具有指數:{"unit" : 1.0}爲什麼MongoDB計算這麼久?

查詢:

db.getCollection('CollectionName').find({"unit":"value"}).count() 

進行5秒(導致200個000文檔)

查詢:

db.getCollection('CollectionName').find({"unit":"value"}) 

執行0.005秒

爲什麼?

回答

3

find()返回一個光標,find().count()用盡該光標以計算其中的文檔數量。

你可能更願意使用db.collection.count,例如:

db.getCollection('CollectionName').count({"unit":"value"}) 
+0

db.collection.count相當於db.collection.find –

+0

它是*功能*等同,但*不*函是不同的。 find(...)。count()會讀取所有200,000個文檔,然後對它們進行計數,而count(...)將執行計數服務器端並只返回與給定過濾器相匹配的文檔數。 – glytching

+0

db.getCollection('CollectionName')。count({「unit」:「value」})dothe't help,query perform 5 sec –