最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I get data from mongoDB between a certain date (date is stored as createdOn in our document)? - Stack Overf

programmeradmin5浏览0评论

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. The main purpose of this code is to get the data between a date which will be downloaded by the user in CSV format.

This is the schema of the 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. The main purpose of this code is to get the data between a date which will be downloaded by the user in CSV format.

This is the schema of the document:

Share asked Oct 31, 2018 at 7:03 pratyush kumarpratyush kumar 631 gold badge1 silver badge9 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

You can use $gte and $lte operators to filter data on a range of date.

In your case to search in Offer collection the query will be:

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)

{createdOn: {$gte: ISODate('START_DATE'),$lte: ISODate('END_DATE')}}

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论