0

I'm currently trying to group a set of documents by the week number of an exit_time field.

I can do so fine with the $week aggregation like so.

$group: {
  _id: {$week: "$exit_time"}
}

However if I try to include the timezone like in the mongodb documentation I get the following error.

MongoError: can't convert from BSON type object to Date

Here's the code snippet that's causing it. As far as I can tell it is how the examples do it in the mongo documentation.

$group: {
  _id: {
    $week: {
      date: "$exit_time",
      timezone: "America/Tijuana"
    }
  }
}

Here's the relevant documentation. https://docs.mongodb.com/manual/reference/operator/aggregation/week/

Has anyone ran into a similar issue that can point me in the right direction?

  • 2
    The error might be a bit misleading, but clearly you don't actually have **MongoDB 3.6**, just like the statement ( *changed in MongoDB 3.6* ) actually says in the documentation before it talks about the addition of timezone conversion. If you don't have MongoDB 3.6 you need to take a mathematical approach to adjusting timezones – Neil Lunn May 04 '18 at 21:32
  • Gory details at [Group by Date with Local Time Zone in MongoDB](https://stackoverflow.com/q/45038711/2313887) and of course generally superseded by the `timezone` option in date operators since MongoDB 3.6, but covers adjustments including shifts in DST to be applied for earlier versions. – Neil Lunn May 05 '18 at 01:58

1 Answers1

0

As Neil Lunn said, the issue for me was that I was actually running a pre 3.6 version of MongoDB (although I thought I was running 3.6).

Aymeric Bouzy aybbyk
  • 2,031
  • 2
  • 21
  • 29