Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB Aggregate Query Group by Dates

I have the following collection in mongoDB

{ _id, startTime, duration }

So the basic idea is that a camera looks out for people and once it detects a person it saves the startTime and once a person disappears it saves the duration. So the entity basically says "A person appeared at X time and was in the camera range for Y milliseconds". Both startTime and duration are numeric values.

So, I want to perform various queries like: 1. Give me the number of people per month/year 2. Give me the number of people per month with duration > 5000ms

etc.

I'm fairly new to MongoDB though and I have a bit of trouble with the aggregation framework, so I would appreciate if someone gives me an idea of how to do a query such as the above, in order to get some sort of head start.

EDIT:

Ok I have done a couple of tries but no luck. Right now my objects have this form:

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

and I'm trying this query:

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

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

but I'm getting the following exception:

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 }
like image 393
ChrisGeo Avatar asked Jan 17 '14 08:01

ChrisGeo


2 Answers

You can do them with Aggregation Framework.

Give me the number of people per month/year

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

Give me the number of people per month with duration > 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"}
    }}
)

For more information check Aggregation Framework.

like image 127
Parvin Gasimzade Avatar answered Sep 28 '22 10:09

Parvin Gasimzade


Please refer to compatible data format of MongoDB at http://docs.mongodb.org/manual/reference/bson-types/#document-bson-type-date

And below is the way to test to aggreagations.

 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>

you can check more related posts at http://www.dbversity.com/

like image 25
Srinivas Kishore Avatar answered Sep 28 '22 10:09

Srinivas Kishore