0

I have some code where I set up the following pipeline: filter collection, project year & month, group by year & month, and then finish with a datetime object like YYYY-MM-01.

Example Document:

{
  _id: 123456
  foo: "bar"
  dt: ISODate("2015-12-24T11:59:00Z")
}

Example Code:

from pymongo import MongoClient
db = client.testDB
posts = db.testCollection
pipeline = [
  {"$match": {"foo":"bar"}},
  {"$project": {
      "year": {"$year": "$dt"},
      "month": {"$month": "$dt"},
    }
  },
  {"$group": {
      "_id": { "dt": ??? },
      "totalCount": { "$sum": 1 }
    }
  },
  {"$out": "myResults"}
}
posts.aggregate(pipeline)

Goal:

{
  _id: {dt: ISODate("2015-12-01T00:00:00Z")}
  totalCount: 8
}
timctran
  • 505
  • 4
  • 10

1 Answers1

0

In order to project dates to the first of the month, e.g., transform 2015-12-24 to 2015-12-01, I modified the code on this page as follows:

Example Document:

{
  _id: 123456
  foo: "bar"
  dt: ISODate("2015-12-24T11:59:00Z")
}

Code:

from pymongo import MongoClient
db = client.testDB
posts = db.testCollection
pipeline = [
  {"$match": {"foo":"bar"}},
  {
    "$project": {
      "dt": "$dt"
      "d": {"$dayOfMonth": "$dt"},
      "h": {"$hour": "$dt"},
      "m": {"$minute": "$dt"},
      "s": {"$second": "$dt"},
      "ml": {"$millisecond": "$dt"},
    }
  },
  {
    "$group": {
      "_id": {
        "$subtract": [
          "$dt",
          {
            "$add": [
              "$ml",
              {"$multiply": ["$s", 1000]},
              {"$multiply": ["$m", 60, 1000]},
              {"$multiply": ["$h", 60, 60, 1000]},
              {"$multiply": [{"$subtract": ["$d", 1]}, 24, 60, 60, 1000]},
            ]
          }
        ] 
      },
      "totalCount": { "$sum": 1 }
    }
  },
  {"$out": "myResults"}
}
timctran
  • 505
  • 4
  • 10