我在mongoDB中有以下集合
{ _id, startTime, duration }
所以基本的想法是摄像机会寻找人,一旦它检测到一个人就会保存startTime,一旦一个人消失,就可以节省持续时间。 因此,该实体基本上说“一个人出现在X时间,并且在摄像机范围内为Y毫秒”。 startTime和duration都是数值。
所以,我想执行各种查询,例如: 1.给我每月/每年的人数 2.给我一个月的人数,持续时间>值为5000ms
等
我对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 :(得分:15)
您可以使用聚合框架来完成它们。
给我每月/每年的人数
db.collection.aggregate(
{$project : {
year : {$year : "$startTime"},
month : {$month : "$startTime"}
}},
{$group : {
_id : {year : "$year", month : "$month"},
count : {$sum : 1}
}}
)
给我一个月的人数,持续时间>值为5000ms
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.
答案 1 :(得分:1)
请参考http://docs.mongodb.org/manual/reference/bson-types/#document-bson-type-date
的MongoDB兼容数据格式以下是测试聚合的方法。
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>
查看更多相关信息