[英]How can I get data from mongoDB between a certain date (date is stored as createdOn in our document)?
This my query - 这是我的查询-
var Query = Offer
.where('isDeleted').equals(false)
.populate('country')
.populate('state')
.populate('city')
.populate('store')
.populate("createdBy")
.populate("main_cat")
.populate("grab_by")
.populate("merchant_id")
.sort('store_name');
if (typeof querydata.country != 'undefined' && querydata.country !== '') {
Query.where('country').in(querydata.country)
}
if (querydata.state.length > 0) {
querydata.state = querydata.state.split(",");
Query.where('state').in(querydata.state);
}
if (querydata.city.length > 0) {
querydata.city = querydata.city.split(",");
Query.where('city').in(querydata.city);
}
//here i am trying to get data from start to end date
if ((req.query.start_date !== '' && typeof req.query.start_date !== 'undefined') && (req.query.end_date !== '' && typeof req.query.end_date !== 'undefined')) {
var startdate = new Date(req.query.start_date);
var enddate = new Date(req.query.end_date);
//(startdate);
enddate.setDate(enddate.getDate() + 1);
Query.where(('createdOn').split('T')[0]).gte(startdate).lte(enddate);
}
In the last if else condition I am trying where I am trying to get between start date and end date. 在其他情况下,我正在尝试尝试在开始日期和结束日期之间到达的位置。 Please suggest how should I write my query for the same. 请建议我该如何写同样的查询。 Should I use momentJS for the same. 我应该同样使用momentJS吗? The main purpose of this code is to get the data between a date which will be downloaded by the user in CSV format. 该代码的主要目的是获取日期之间的数据,该日期将由用户以CSV格式下载。
You can use $gte
and $lte
operators to filter data on a range of date. 您可以使用$gte
和$lte
运算符来过滤日期范围内的数据。
In your case to search in Offer
collection the query will be: 在你的情况下,搜索Offer
集合查询将是:
db.Offer.find({createdOn: {$gte: ISODate('START_DATE'),$lte: ISODate('END_DATE')}})
As you are using MongoDB Compass just put following in the filter
the filter field: (in Schema tab) 在使用MongoDB Compass时,只需在filter
的过滤器字段中输入以下内容:(在“架构”选项卡中)
{createdOn: {$gte: ISODate('START_DATE'),$lte: ISODate('END_DATE')}}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.