2014-01-17 88 views
5

我在MongoDB中以下收集MongoDB的彙總查詢集團按日期

{ _id, startTime, duration } 

所以基本思想是,一個攝像頭俯瞰的人,一旦檢測到它節省了startTime和一個人一個人一旦消失它可以節省持續時間。 所以實體基本上說「一個人出現在X時間,並在Y毫秒的相機範圍內」。 startTime和duration都是數字值。

所以,我要像執行各種查詢: 1.給我每月的人數/年 2.給我的人數每月與持續時間> 5000毫秒

雖然我對MongoDB相當陌生,但是我對聚合框架有點麻煩,所以如果有人給我一個關於如何進行上述查詢的想法,以便獲得某種類型的開始。

編輯:

好吧,我已經做了幾次嘗試,但沒有運氣。現在,我的對象具有這種形式:

{ 
    "_id" : ObjectId("52de407c75895eaf5ea99715"), 
    "startTime" : "new Date('02 01 2011 08:36:54')", 
    "duration" : 27000 
} 

,我想這個查詢:

db.collection.aggregate(
     {$project : { 
      year : {$year : "$startTime"} 

     }}, 
     {$group : { 
      _id : {year : "$year"}, 
      count : {$sum : 1} 
     }} 
    ) 

,但我發現以下異常:

Error occurred in performing aggregation 
Command 'aggregate' failed: exception: can't convert from BSON type String to Date (response: { "errmsg" : "exception: can't convert from BSON type String to Date", "code" : 16006, "ok" : 0.0 }) 
Type: MongoDB.Driver.MongoCommandException 
Stack: at MongoDB.Driver.Operations.CommandOperation`1.Execute(MongoConnection connection) 
    at MongoDB.Driver.MongoCollection.RunCommandAs[TCommandResult](IMongoCommand command, IBsonSerializer resultSerializer, IBsonSerializationOptions resultSerializationOptions) 
    at MongoDB.Driver.MongoCollection.RunCommandAs[TCommandResult](IMongoCommand command) 
    at MongoDB.Driver.MongoCollection.Aggregate(IEnumerable`1 operations) 
    at MangoUI.ComAggregate.kRemove_Click(Object sender, EventArgs e) 
Inputs:: 
Command: aggregate 
Ok:  False 
ErrorMsg: exception: can't convert from BSON type String to Date 
Request: { "aggregate" : "person", "pipeline" : [{ "$project" : { "year" : { "$year" : "$startTime" } } }, { "$group" : { "_id" : { "year" : "$year" }, "count" : { "$sum" : 1 } } }] } 
Response: { "errmsg" : "exception: can't convert from BSON type String to Date", "code" : 16006, "ok" : 0.0 } 
+0

檢查更多相關的帖子你可以通過分組的這個MongoDB的文件:http://docs.mongodb.org/manual/reference/method/db.collection.group/ –

+0

如果您想要進行這些類型的查詢,您應該將您的文檔'startTime'更改爲'Date'而不是數字。 – JohnnyHK

回答

14

你可以做他們與聚合框架。

給我每月的人數/年

db.collection.aggregate(
    {$project : { 
     year : {$year : "$startTime"}, 
     month : {$month : "$startTime"} 
    }}, 
    {$group : { 
     _id : {year : "$year", month : "$month"}, 
     count : {$sum : 1} 
    }} 
) 

給我每月人持續時間> 5000毫秒

db.collection.aggregate(
    {$project : { 
     year : {$year : "$startTime"}, 
     month : {$month : "$startTime"}, 
     duration: {$cond: [{$gt: ['$duration', 5000]}, 1, 0]} 
    }}, 
    {$group : { 
     _id : {year : "$year",month : "$month"}, 
     duration : {$sum : "$duration"} 
    }} 
) 

欲瞭解更多信息,數量檢查Aggregation Framework.

+0

我嘗試了第一個,但我似乎得到以下異常:命令'聚合'失敗:例外:不能從BSON類型NumberLong64轉換爲日期(響應:{「errmsg」:「異常:無法轉換從BSON類型NumberLong64到Date「,」code「:16006,」ok「:0.0}) – ChrisGeo

+0

我忘了提及。爲了使用$ year和$ month操作符,startTime應該使用日期格式。它不適用於Long。 –

+0

我可以即時進行轉換嗎?我嘗試了幾種方法,但似乎沒有任何工作。 – ChrisGeo

1

請參考MongoDB的兼容數據格式http://docs.mongodb.org/manual/reference/bson-types/#document-bson-type-date

以下是測試聚合的方法。

rs1:PRIMARY> 
rs1:PRIMARY> db.dbversitycol.insert({ "_id" : "1", "LastUpdatedOn" : new Date() , "company" : "microsoft" }) 
rs1:PRIMARY> db.dbversitycol.insert({ "_id" : "2", "LastUpdatedOn" : new Date() , "company" : "google" }) 
rs1:PRIMARY> db.dbversitycol.insert({ "_id" : "3", "LastUpdatedOn" : new Date() , "company" : "ibm" }) 
rs1:PRIMARY> db.dbversitycol.insert({ "_id" : "4", "LastUpdatedOn" : new Date() , "company" : "cisco" }) 
rs1:PRIMARY> db.dbversitycol.insert({ "_id" : "5", "LastUpdatedOn" : new Date() , "company" : "dbversity.com" }) 
rs1:PRIMARY> 
rs1:PRIMARY> db.dbversitycol.find() 
{ "_id" : "1", "LastUpdatedOn" : ISODate("2014-11-28T13:09:13.203Z"), "company" : "microsoft" } 
{ "_id" : "2", "LastUpdatedOn" : ISODate("2014-11-28T13:09:13.207Z"), "company" : "google" } 
{ "_id" : "3", "LastUpdatedOn" : ISODate("2014-11-28T13:09:13.210Z"), "company" : "ibm" } 
{ "_id" : "4", "LastUpdatedOn" : ISODate("2014-11-28T13:09:13.213Z"), "company" : "cisco" } 
{ "_id" : "5", "LastUpdatedOn" : ISODate("2014-11-28T13:09:14.035Z"), "company" : "dbversity.com" } 
rs1:PRIMARY> 
rs1:PRIMARY> 
rs1:PRIMARY> db.dbversitycol.aggregate(
... { 
...  "$project" : 
...  { 
...  _id : 0, 
...  "datePartDay" : {"$concat" : [ 
...   {"$substr" : [{"$dayOfMonth" : "$LastUpdatedOn"}, 0, 2]}, "-", 
...   {"$substr" : [{"$month" : "$LastUpdatedOn"}, 0, 2]}, "-", 
...   {"$substr" : [{"$year" : "$LastUpdatedOn"}, 0, 4]} 
...  ] } 
...  } 
... }, 
... { "$group" : 
...  { "_id" : "$datePartDay", "Count" : { "$sum" : 1 } } 
...  } 
...) 
{ "result" : [ { "_id" : "28-11-2014", "Count" : 5 } ], "ok" : 1 } 
rs1:PRIMARY> 
rs1:PRIMARY> 



rs1:PRIMARY> db.dbversitycol.aggregate(
...  {$project : { 
...   year : {$year : "$LastUpdatedOn"}, 
...   month : {$month : "$LastUpdatedOn"} 
...  }}, 
...  {$group : { 
...   _id : {year : "$year", month : "$month"}, 
...   count : {$sum : 1} 
...  }} 
...) 
{ 
     "result" : [ 
       { 
         "_id" : { 
           "year" : 2014, 
           "month" : 11 
         }, 
         "count" : 5 
       } 
     ], 
     "ok" : 1 
} 
rs1:PRIMARY> 

你可以在http://www.dbversity.com/