2017-02-21 96 views
0

我收集的名字是transactions。 我分享交易徵收的對象如何在mongodb中查詢以獲得明顯的記錄數

{ 
    "_id" : ObjectId("58aaec83f1dc6914082afe31"), 
    "amount" : "33.00", 
    "coordinates" : { 
     "lat" : "4.8168", 
     "lon" : "36.4909" 
    }, 
    "cuisine" : "Mexican", 
    "date" : ISODate("0062-02-22T11:46:52.738+05:30"), 
    "location" : { 
     "address" : "2414 Trudie Rue", 
     "city" : "West Alisa", 
     "state" : "New York", 
     "zip" : "10000" 
    }, 
    "place_name" : "Outdoors", 
    "place_type" : "Wooden" 
}, 
{ 
    "_id" : ObjectId("58aaec83f1dc6914082afe32"), 
    "amount" : "557.00", 
    "coordinates" : { 
     "lat" : "-36.6784", 
     "lon" : "131.3698" 
    }, 
    "cuisine" : "Australian", 
    "date" : ISODate("1294-10-04T19:53:15.562+05:30"), 
    "location" : { 
     "address" : "5084 Buckridge Cove", 
     "city" : "Sylviaview", 
     "state" : "Hawaii", 
     "zip" : "51416-6918" 
    }, 
    "place_name" : "Toys", 
    "place_type" : "Cotton" 
}, 
{ 
    "_id" : ObjectId("58aaec83f1dc6914082afe33"), 
    "amount" : "339.00", 
    "coordinates" : { 
     "lat" : "45.1468", 
     "lon" : "91.4097" 
    }, 
    "cuisine" : "Mexican", 
    "date" : ISODate("1568-11-25T02:54:53.046+05:30"), 
    "location" : { 
     "address" : "94614 Harry Island", 
     "city" : "Cartwrightside", 
     "state" : "Louisiana", 
     "zip" : "18825" 
    }, 
    "place_name" : "Clothing", 
    "place_type" : "Frozen" 
}, 
{ 
    "_id" : ObjectId("58aaec83f1dc6914082afe34"), 
    "amount" : "173.00", 
    "coordinates" : { 
     "lat" : "-57.2738", 
     "lon" : "19.6381" 
    }, 
    "cuisine" : "Australian", 
    "date" : ISODate("0804-05-07T03:00:07.724+05:30"), 
    "location" : { 
     "address" : "1933 Lewis Street", 
     "city" : "Aufderharville", 
     "state" : "Louisiana", 
     "zip" : "23416" 
    }, 
    "place_name" : "Beauty", 
    "place_type" : "Fresh" 
}, 
{ 
    "_id" : ObjectId("58aaec83f1dc6914082afe34"), 
    "amount" : "173.00", 
    "coordinates" : { 
     "lat" : "-57.2738", 
     "lon" : "19.6381" 
    }, 
    "cuisine" : "Australian", 
    "date" : ISODate("0804-05-07T03:00:07.724+05:30"), 
    "location" : { 
     "address" : "1933 Lewis Street", 
     "city" : "Aufderharville", 
     "state" : "Louisiana", 
     "zip" : "23416" 
    }, 
    "place_name" : "Beauty", 
    "place_type" : "Fresh" 
} 

我想要得到的不同美食列表與總數

輸出

{ 
    "name" : 'Mexican', 
    "count" : '2' 
}, 
{ 
    "name" : 'Australian', 
    "count" : '3' 
}, 

我能有用mysql輕鬆完成,但我知道在mongo DB因爲我有新的mongodb

我曾試圖與榜樣,我什麼也沒找到:

db.transactions.aggregate(
    {$group: {_id:'$cuisine'},count:{$sum:1}} 
    ).result; 
+0

的可能的複製[MongoDB的SELECT COUNT(DISTINCT x)的上一個索引列 - 計算大數據集的唯一結果](http://stackoverflow.com/questions/11782566/mongodb-select-countdistinct-x -on-an-indexed-column-count-unique-results-for) – hassan

+0

感謝@HassanAhmed的評論,但我沒有發現這篇文章非常有幫助 –

回答

1

請嘗試以下的代碼。你應該通過美食來記錄這些記錄並計算它們的數量。稍後在項目管道中,您可以定義最終外觀。

db.transactions.aggregate([ 
{ $group: { _id: "$cuisine", count: { $sum: 1 } } }, 
{ $project:{ _id: 0, name: "$_id", count:"$count" } } 
]); 
+0

哦謝謝你@hasan這就是我正在尋找的,但你能向我解釋它是如何工作的以及我做錯了什麼? –

+0

首先,聚合框架與流水線階段一起工作,所以聚合函數將一個數組作爲參數。在這個數組中,你應該定義流水線階段。在你的問題中,第一階段必須是組功能,並且你做得很好。在第二階段,你必須使用項目函數來重新構造查詢結果。例如第一階段小組賽將返回類似的東西: { 「_id」: '墨西哥人', 「計數」: '2' }, { 「_id」: '澳大利亞', 「計數」:「 3' }, 項目階段正在重塑它,就像你得到結果一樣。 –

相關問題