我在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 }
檢查更多相關的帖子你可以通過分組的這個MongoDB的文件:http://docs.mongodb.org/manual/reference/method/db.collection.group/ –
如果您想要進行這些類型的查詢,您應該將您的文檔'startTime'更改爲'Date'而不是數字。 – JohnnyHK