camelCaseWarrior
camelCaseWarrior

Reputation: 369

Mongo date aggregation operators with ObjectId

I'm trying to use the ObjectId as a creation date holder, and running into some problems when trying to do aggregation queries. In particular we want to use the date aggregation operators to group the documents by month, but that operator doesn't work with ObjectId apparently. Is there a way around it, or would I have to start using a separate CreationTime field per document?

Here is a query I was trying -

db.People.aggregate([
{
    $match: {
        $or: [{"Name":"Random1"}, {"Name":"Random2"}]
    }
},
{
    $project: {
        Name: "$Name",
        Year: {$year: "$_id"},
        Month: {$month: "$_id"}
    }
},
{
    $group: {
        _id: {Name: "$Name", Year: "$Year", Month: "$Month"},
        TotalRequests: {$sum:1}
    }
}

])

Upvotes: 0

Views: 1958

Answers (1)

Derick
Derick

Reputation: 36794

Right now, you need to keep a separate field as the Aggregation Framework can not deal with this yet. There is a server ticket at https://jira.mongodb.org/browse/SERVER-9406 to implement it so I suggest you vote for it (I just did).

Upvotes: 4

Related Questions