Mongo DB ISO格式 [英] Mongo DB ISO format
问题描述
基于下面的查询返回结果,我想过滤月份和年份。
Based on the below query return result I want to filter the month and ther year.
例如,我只想要3月份的数据。
For example I only want data for the month of March.
db.SBM_USER_DETAIL.aggregate([
{
$project: {
join_date: '$JOIN_DATE'
}
}
]).map(
function(d) {
d.join_date = moment(d.join_date).locale('es').tz("Asia/Kolkata").format();
return d
})
如何在MongoDB聚合查询中使用 join_date 的返回格式值?
How to use the returned formatted value of join_date inside the MongoDB aggregation query?
推荐答案
MongoDB的ISODate与javascript Date类非常相似。如果您在加尔各答所在的时区有一个日期范围,并希望按该日期范围进行过滤,请在运行查找之前实例化一对Date对象以定义范围。
MongoDB's ISODate is very similar to the javascript Date class. If you have a date range in the Kolkata timezone, and want to filter by that, instantiate a pair of Date objects to define the range, before running the find.
在这种情况下,要返回2017年3月以内的所有 join_date
值,转换为加尔各答(UTC- 07:00)时区,过滤大于或等于3月1日午夜且小于4月1日午夜的日期,然后使用时刻转换结果:
For this instance, to return all join_date
values that fall within March 2017, converted to the Kolkata (UTC-07:00) timezone, filter for date greater than or equal to midnight March 1 and less than midnight April 1, then convert the results using moment:
var first = new Date("2017-03-01T00:00:00-07:00");
var last = new Date("2017-04-01T00:00:00-07:00");
db.SBM_USER_DETAIL.find(
{join_date:{$gte: first, $lt: last}}, //filter based on join_date
{join_date:1,_id:0} // only return join_date, omit this if you need all fields
).map(
function(d) {
d.join_date = moment(d.join_date).locale('es').tz("Asia/Kolkata").format();
return d;
}
);
这篇关于Mongo DB ISO格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!