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 badges1 Answer
Reset to default 5You 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')}}