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

This my query - var Query = Offer.where('isDeleted').equals(false).populate('country

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')}}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742264087a4411452.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信