jonlil / mongoose-paginate

mongoose-paginate
MIT License
26 stars 1 forks source link

How do I add a filter by date option to the query? #9

Open bluerid opened 6 years ago

bluerid commented 6 years ago

I need to send in a date range to filter docs returning list of events date wise. How would I be able to do this? Sending the filter parameter with {params} doesn't do anything.

params.date_event = {
      "$gte": _startdate,
      "$lt": _enddate
}
       Event.paginate(params, '_id')
          .execPagination(function(err, events) {
            if(err)
              res.send(err)

              if(events.results.length){
                var dates = []
                var results = []
                events.results.forEach((current) => {
                  var currentDate = current['date_event'].split('T')[0]
                  if(!dates.includes(currentDate)){
                    dates.push(currentDate);
                    results.push ({
                      date: currentDate,
                      items: []
                    });
                  }
                  var dateIdx = dates.indexOf(currentDate)
                  results[dateIdx].items.push(current)
                })
                events.results = results
              }
              res.json({message: "Retrieved Events", events: events})
        });
resistancecanyon commented 6 years ago

@bluerid You could easily add a where clause like this, just like you run it over a mongoose query. Remember this is a mongoose plugin :smile: Like so below filter.date_event = { "$gte": _startdate, "$lt": _enddate } paginationParams = { after: "52fb4cd4205626aceddc7127" }; Event.paginate(paginationParams, '_id') .where(filter) .execPagination(function(err, events) { //your stuff here })