【发布时间】:2021-10-17 14:08:24
【问题描述】:
我有安装了 MongoDB 5.0 的 Ubuntu 20.04 服务器并由 official tutorial 运行。我遇到了一些时区的问题,例如 Etc/UTC 时区。我收到错误“无法识别的时区标识符”:
> db.data.insert({ dt: new ISODate() })
WriteResult({ "nInserted" : 1 })
> db.data.aggregate([
... {
... "$project": {
... "dtTimezone": {
... "$dateToString": {
... "date": "$dt",
... "timezone": "Etc/UTC"
... }
... }
... }
... }
... ])
uncaught exception: Error: command failed: {
"ok" : 0,
"errmsg" : "PlanExecutor error during aggregation :: caused by :: unrecognized time zone identifier: \"Etc/UTC\"",
"code" : 40485,
"codeName" : "Location40485"
} with original command request: {
"aggregate" : "data",
"pipeline" : [
{
"$project" : {
"dtTimezone" : {
"$dateToString" : {
"date" : "$dt",
"timezone" : "Etc/UTC"
}
}
}
}
],
"cursor" : {
},
"lsid" : {
"id" : UUID("24a0ca58-6d03-4827-b59c-4bd82ddf9976")
}
} on connection: connection to 127.0.0.1:27017 : aggregate failed :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
doassert@src/mongo/shell/assert.js:18:14
_assertCommandWorked@src/mongo/shell/assert.js:719:17
assert.commandWorked@src/mongo/shell/assert.js:811:16
DB.prototype._runAggregate@src/mongo/shell/db.js:276:5
DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1058:12
@(shell):1:1
同时,在使用欧洲/伦敦时,一切正常:
> db.data.aggregate([
... {
... "$project": {
... "dtTimezone": {
... "$dateToString": {
... "date": "$dt",
... "timezone": "Europe/London"
... }
... }
... }
... }
... ])
{ "_id" : ObjectId("611832f076340952c425ce80"), "dtTimezone" : "2021-08-14T22:17:36.349Z" }
我找到了similar problem,但解决方案是注释“processManagement.timeZoneInfo”选项并使用内部时区数据库。我也尝试从MongoDB documentation 复制时区数据库,但没有任何区别:
curl -O https://downloads.mongodb.org/olson_tz_db/timezonedb-latest.zip
unzip timezonedb-latest.zip
rsync -a --delete timezonedb-2021a/ /usr/share/zoneinfo/
systemctl restart mongod
可能是什么问题?为什么系统数据库不适合 MongoDB 或无法与 MongoDB 一起使用?
【问题讨论】:
-
我不认为“Etc/UTC”是有效的时区名称(在奥尔森时区数据库中)使用“UTC”或跳过此参数,因为 UTC 是默认值。
-
@WernfriedDomscheit 根据this list,这是正确的时区。使用内部时区数据库时,通过注释“processManagement.timeZoneInfo 选项”执行使用 Etc/UTC 时区的查询。我可以忽略 Etc/UTC,因为它与 UTC 相同,但我的数据有 Etc/GMT+12 时区,我不能忽略。
-
用
+12:00代替Etc/GMT+12怎么样?都是一样的,只是格式不同。 -
@WernfriedDomscheit 时区等/GMT+12 等于 UTC-12。这些数据来自外部来源,我必须对其进行处理,并且我想使用 MongoDB 功能来处理它。我需要一个通用的解决方案。
-
是否可以选择使用外部库,例如moment.js?
标签: mongodb ubuntu-20.04