Strange behavior in MongoDB date attribute when grouping by Month

I'm getting wrong data in line chart. My data start at 03/2021 and chart is showing as 02/2021

That is the query that Metabase has generated.

Running it at MongoDB compass works like expected

Hi @trialsin
Post "Diagnostic Info" from Admin > Troubleshooting.
You are seeing a timezone issue, they are complicated and complex. Read this:
https://www.metabase.com/docs/latest/troubleshooting-guide/timezones.html

Hi @flamber!

I've just two timezone information...

  1. The dates are stored in UTC with timezone information like it: ISODate("2021-03-18T19:37:12.955Z")
  2. I need the report to be displayed in my local timezone: GMT-3 (America/Araguaina).

So I set both the -e "JAVA_TIMEZONE=America/Araguaina" and the report configuration

image

{
  "browser-info": {
    "language": "pt-BR",
    "platform": "Win32",
    "userAgent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36",
    "vendor": "Google Inc."
  },
  "system-info": {
    "file.encoding": "UTF-8",
    "java.runtime.name": "OpenJDK Runtime Environment",
    "java.runtime.version": "11.0.13+8",
    "java.vendor": "Eclipse Adoptium",
    "java.vendor.url": "https://adoptium.net/",
    "java.version": "11.0.13",
    "java.vm.name": "OpenJDK 64-Bit Server VM",
    "java.vm.version": "11.0.13+8",
    "os.name": "Linux",
    "os.version": "5.10.16.3-microsoft-standard-WSL2",
    "user.language": "en",
    "user.timezone": "America/Araguaina"
  },
  "metabase-info": {
    "databases": [
      "h2",
      "mongo"
    ],
    "hosting-env": "unknown",
    "application-database": "h2",
    "application-database-details": {
      "database": {
        "name": "H2",
        "version": "1.4.197 (2018-03-18)"
      },
      "jdbc-driver": {
        "name": "H2 JDBC Driver",
        "version": "1.4.197 (2018-03-18)"
      }
    },
    "run-mode": "prod",
    "version": {
      "date": "2021-12-16",
      "tag": "v0.41.5",
      "branch": "release-x.41.x",
      "hash": "fbfffc6"
    },
    "settings": {
      "report-timezone": "America/Araguaina"
    }
  }
}

@trialsin But if you are using Native query, then you'll need to do the conversion yourself.

I'm not using native query. I'm using Simple Question. The query that I've posted was generated by Metabase:

I've just made a summary in a Simple Question... Is it the same as a native query?

@trialsin That was impossible to tell from your previous snipped screenshots.

Have you tried starting without the JVM timezone, or explicitly setting it to UTC?

Again, timezones are complicated - very complicated - it's difficult to know all the different timezones you might have, or if the problem is frontend or backend.

I've changed to metabase jar on windows to do more tests...

I'm connecting direct to mongo collection and results are:

When showing plain collection, dates are shown as expected, reflecting the defined timezone:

With java -Duser.timezone=UTC -jar metabase.jar
image

With java -Duser.timezone=(America/Araguaina or GMT-3) -jar metabase.jar
image

But when doing summary it get crazy if timezone is not UTC!

With java -Duser.timezone=UTC -jar metabase.jar
image

With java -Duser.timezone=(America/Araguaina or GMT-3) -jar metabase.jar
image

When writing this post, I've found the problem! :smile: :raised_hands::pray: It is a bug in the generated query:

The query generated by metabase when summaring, use the operator $dateFromParts without any timezone information. And I think that the default is UTC (Mongo Documentation) because the documentation of $dateToParts says that

So, if the desired timezone is set to that function, everything works:

Original query generated by Metabase:

Modified query with timezone included:

The full query:

[
    {
        "$match": {
            "t": "l"
        }
    },
    {
        "$group": {
            "_id": {
                "ts~~~month": {
                    "$let": {
                        "vars": {
                            "parts": {
                                "$dateToParts": {
                                    "date": "$ts"
                                }
                            }
                        },
                        "in": {
                            "$dateFromParts": {
                                "year": "$$parts.year",
                                "month": "$$parts.month"
                                "timezone" : "America/Araguaina"
                            }
                        }
                    }
                }
            },
            "count": {
                "$sum": 1
            }
        }
    },
    {
        "$sort": {
            "_id": 1
        }
    },
    {
        "$project": {
            "_id": false,
            "ts~~~month": "$_id.ts~~~month",
            "count": true
        }
    },
    {
        "$sort": {
            "ts~~~month": 1
        }
    }
]

@trialsin I'm guessing you're seeing this issue:
https://github.com/metabase/metabase/issues/11149 - upvote by clicking :+1: on the first post

Okay!

I think that post will be useful for the solution. I'll reference it in the issue