mongo 查询中的时区
Timezone in mongo query
我在 mongodb 中插入了 UTC 时间格式的数据。我希望根据时区转换时间。有没有可能在 mongo 查询中这样做?
假设您的文档包含 ISODate
如下:
db.collection.insert({"date":new Date()})
上面的查询在 ISODate
格式中插入 date
现在你想把这个 ISODate
转换成 timeZone
。
假设您要将上述日期转换为 Eastern Daylight Saving Time ( EDT )
epoch time zone conertor,然后 offset
转换为 14400 * 1000
。首先将 ISODate
转换为 timeStamp
,然后再次使用 substract
EDT Offsetin
timeStampand then convert
timeStampto
ISODate`。
检查以下聚合查询:
db.collection.aggregate({
"$project": {
"timestamp": { //convert ISODate tom timestamp
"$subtract": [{
"$divide": [{
"$subtract": ["$date", new Date("1970-01-01")]
}, 1000]
}, {
"$mod": [{
"$divide": [{
"$subtract": ["$date", new Date("1970-01-01")]
}, 1000]
}, 1]
}]
}
}
}, {
"$project": {
"timeZoneTimeStamp": {
"$subtract": [{ //substract timestamp to given offset if offset will in postive then replace subtract to add
"$multiply": ["$timestamp", 1000]
}, 14400000]
}
}
}, {
"$project": {
"timeZoneTimeStamp": 1, //converted timeZoneTimeStamp if required
"_id": 0,
"newDate": { // newDate is converted timezone ISODate
"$add": [new Date(0), "$timeZoneTimeStamp"]
}
}
})
注意:
在上面的查询转换中,从 ISODATE
到 timeStamp
ref. here
如果日期未更改且保持不变,例如类似于 created_record_date 那么无论您需要哪个时区数据,您都应该预先计算并保存(作为字符串)以及同一文档,这样您就不必 运行 在 运行time 这可能会减慢执行时间。如果您有现有记录并且想要将各种不同的时区数据与记录一起存储,请考虑 运行 进行 Map-Reduct 作业并分别更新文档。 (如果您需要该代码,请告诉我)。但是,如果可以根据业务逻辑更改此日期字段,那么在 运行 时间计算是明智的。这两种技术都有不同的用例和优缺点。
-$
在 mongo 版本 3.6 中添加了时区,mongo doc
提取带有时区的日期部分的表达式是
{ date: <dateExpression>, timezone: <tzExpression> }
我们可以在获取日期部分时指定时区或偏移量。
查看我发布的答案
从带时区的日期获取日期 America/Chicago
{ $month: {
date: new Date(),
timezone: "America/Chicago"
} }
或带偏移量
{ $month: {
date: ISODate(),
timezone: "-0500"
} }
如果您使用的是猫鼬(可能也适用于本机驱动程序):
import moment from 'moment-timezone'; // this is needed to use .tz() method
import mongoMoment from 'mongodb-moment';
// Initalize mongodb-moment so you can use moment() object directly in mongo query
mongoMoment(moment);
// Add timezone to your_date
const date = moment(your_date)
.tz("Europe/Zagreb");
// Make $gte/$lte queries with date ...
我在 mongodb 中插入了 UTC 时间格式的数据。我希望根据时区转换时间。有没有可能在 mongo 查询中这样做?
假设您的文档包含 ISODate
如下:
db.collection.insert({"date":new Date()})
上面的查询在 ISODate
格式中插入 date
现在你想把这个 ISODate
转换成 timeZone
。
假设您要将上述日期转换为 Eastern Daylight Saving Time ( EDT )
epoch time zone conertor,然后 offset
转换为 14400 * 1000
。首先将 ISODate
转换为 timeStamp
,然后再次使用 substract
EDT Offsetin
timeStampand then convert
timeStampto
ISODate`。
检查以下聚合查询:
db.collection.aggregate({
"$project": {
"timestamp": { //convert ISODate tom timestamp
"$subtract": [{
"$divide": [{
"$subtract": ["$date", new Date("1970-01-01")]
}, 1000]
}, {
"$mod": [{
"$divide": [{
"$subtract": ["$date", new Date("1970-01-01")]
}, 1000]
}, 1]
}]
}
}
}, {
"$project": {
"timeZoneTimeStamp": {
"$subtract": [{ //substract timestamp to given offset if offset will in postive then replace subtract to add
"$multiply": ["$timestamp", 1000]
}, 14400000]
}
}
}, {
"$project": {
"timeZoneTimeStamp": 1, //converted timeZoneTimeStamp if required
"_id": 0,
"newDate": { // newDate is converted timezone ISODate
"$add": [new Date(0), "$timeZoneTimeStamp"]
}
}
})
注意:
在上面的查询转换中,从 ISODATE
到 timeStamp
ref. here
如果日期未更改且保持不变,例如类似于 created_record_date 那么无论您需要哪个时区数据,您都应该预先计算并保存(作为字符串)以及同一文档,这样您就不必 运行 在 运行time 这可能会减慢执行时间。如果您有现有记录并且想要将各种不同的时区数据与记录一起存储,请考虑 运行 进行 Map-Reduct 作业并分别更新文档。 (如果您需要该代码,请告诉我)。但是,如果可以根据业务逻辑更改此日期字段,那么在 运行 时间计算是明智的。这两种技术都有不同的用例和优缺点。
-$
在 mongo 版本 3.6 中添加了时区,mongo doc
提取带有时区的日期部分的表达式是
{ date: <dateExpression>, timezone: <tzExpression> }
我们可以在获取日期部分时指定时区或偏移量。
查看我发布的答案
从带时区的日期获取日期 America/Chicago
{ $month: {
date: new Date(),
timezone: "America/Chicago"
} }
或带偏移量
{ $month: {
date: ISODate(),
timezone: "-0500"
} }
如果您使用的是猫鼬(可能也适用于本机驱动程序):
import moment from 'moment-timezone'; // this is needed to use .tz() method
import mongoMoment from 'mongodb-moment';
// Initalize mongodb-moment so you can use moment() object directly in mongo query
mongoMoment(moment);
// Add timezone to your_date
const date = moment(your_date)
.tz("Europe/Zagreb");
// Make $gte/$lte queries with date ...