2016-02-06 50 views
1

我正在使用MongoDB 3.2。MongoDB按內部陣列分組的集合計數

有這個查詢的麻煩......

(道歉提前爲類似於SQL的語法,我只是學習蒙戈從SQL來)

我需要找到的文件,其中COUNT

"audit_event_type_id" : 1 
shop2.date" BETWEEN "2014-10-01" AND "2015-09-01" 

通過

shop2.facility_name 
MONTH(shop2.date) 
YEAR(shop2.date) 

順序分組它通過

count DESC 

有沒有辦法在查詢中將字符串轉換爲日期?我知道我將shop2.date作爲字符串存儲,當它應該是ISODate。如果沒有,我們假設它是一個ISODate。

下面是一個文檔樣本:

{ 
     "_id" : ObjectId("56b38f31967a337c432119cb"), 
     "audit_event_id" : 12382306, 
     "audit_event_type_id" : 2, 
     "group_id" : 3333489, 
     "applicant_id" : 3428508, 
     "service_credit_id" : 3804844, 
     "page_hit_id" : 43870954, 
     "shop1" : { 
      "facility_id" : 28, 
      "facility_name" : "Fake1", 
      "date" : "2014-08-13", 
      "time" : "07:00", 
      "expedite" : false, 
      "collect_biometrics" : false, 
      "block_id" : 364814, 
      "appointment_category" : "Renewal" 
     }, 
     "shop2" : { 
      "facility_id" : 29, 
      "facility_name" : "Fake2", 
      "date" : "2014-08-07", 
      "time" : "07:00", 
      "expedite" : false, 
      "block_id" : 373614, 
      "appointment_category" : "Biometrics" 
     }, 
     "created_at" : "2014-07-30 00:44:36", 
     "updated_at" : "2014-07-30 00:44:36", 
     "user_id" : 3242890, 
     "payment_id" : null, 
     "mission_id" : 24, 
     "affected_user_id" : null 
    } 

任何幫助,將不勝感激。謝謝!!

更新

我已經更新了所有shop1.date & shop2.date到ISODate。這裏是新的文件樣本:

{ 
     "_id" : ObjectId("56b38f31967a337c432119cb"), 
     "audit_event_id" : 12382306, 
     "audit_event_type_id" : 2, 
     "group_id" : 3333489, 
     "applicant_id" : 3428508, 
     "service_credit_id" : 3804844, 
     "page_hit_id" : 43870954, 
     "shop1" : { 
      "facility_id" : 28, 
      "facility_name" : "Fake1", 
      "date" : ISODate("2014-08-13T00:00:00Z"), 
      "time" : "07:00", 
      "expedite" : false, 
      "collect_biometrics" : false, 
      "block_id" : 364814, 
      "appointment_category" : "Renewal" 
     }, 
     "shop2" : { 
      "facility_id" : 29, 
      "facility_name" : "Fake2", 
      "date" : ISODate("2014-08-07T00:00:00Z"), 
      "time" : "07:00", 
      "expedite" : false, 
      "block_id" : 373614, 
      "appointment_category" : "Biometrics" 
     }, 
     "created_at" : "2014-07-30 00:44:36", 
     "updated_at" : "2014-07-30 00:44:36", 
     "user_id" : 3242890, 
     "payment_id" : null, 
     "mission_id" : 24, 
     "affected_user_id" : null 
    } 
+0

這將是最好的,如果你能轉換日期字段,以正確的日期對象而不是字符串爲您聚合管道生效 – chridam

+0

是@chridam你是對的。我現在正在這樣做。在使用foreach語句將所有日期字符串更新爲ISODates之前,我正在使用mongodump。 – Moody

回答

1

可以使用aggregation framework MongoDB中此:

db.Collection.aggregate([ 
{ $match: { 
    audit_event_type_id: 1, 
    "shop2.date": { $gte: ISODate("2014-10-01T00:00:00.000Z"), $lt: ISODate("2015-09-01T00:00:00.000Z") } } 
}, 
{ $group : { 
     _id : { "shop2.facility_name": "$shop2.facility_name", month: { $month: "$shop2.date" }, year: { $year: "$shop2.date" } }, 
     count: { $sum: 1 } } 
}, 
{ $sort : { count: -1 } } 
]) 
+0

謝謝@Irakai偉大的作品 – Moody