2016-06-07 99 views
0

我知道這可能很簡單,只是幫助我使用reduce函數來計算使用文檔的長度。如何在mongodb查詢中計算基於文檔長度

我有很大的數據集,我想要計算鍵值對的數量爲44的記錄數以及那些低於它的記錄數。

示例。把我收集的

Obj1 - {44 fields} Type Object 
Obj2 - {44 fields} Type Object 
Obj3 - {44 fields} Type Object 
Obj4 - {44 fields} Type Object 
Obj5 - {2 fields} Type Object 
Obj6 - {1 fields} Type Object 
Obj7 - {44 fields} Type Object 

我想要得到的計數5 44和2 <的44

+0

可能的複製在MongoDB中大小大於1(1)](http://stackoverflow.com/questions/7811163/how-to-query-for-documents-where-array-size-is-greater-than-one-1-在-的MongoDB) – Shrabanee

回答

0

試試這個:如何查詢文件,其中陣列的

> var equals_to_44 = 0; 
> var non_equal_to_44 = 0; 
> db.your_collection.find().forEach(function(x) { count=0; for(field in x) { count++; } if (count==44){equals_to_44++;} else {non_equal_to_44++;} })