I am trying to fill a chart of data, the chart takes a number of orders each day of the past 7 days.
How can I query and group the past 7 days?
Sorry if its a bit complicated to understand.
I am trying to fill a chart of data, the chart takes a number of orders each day of the past 7 days.
How can I query and group the past 7 days?
Sorry if its a bit complicated to understand.
You use the relativeTime operator which makes it very easy:
const query = new Parse.Query('Order');
query.greaterThan('createdAt', { $relativeTime: '7 days ago' });
const results = await query.find();
This operator is badly documented but was introduced a while ago and is fully tested.
You can either use the 'n days ago' or 'in n days'
You can also see the test suite here:
https://github.com/parse-community/parse-server/blob/master/spec/ParseQuery.spec.js#L4155
Get the date which is 7 days ago:
let d = new Date();
d.setDate(d.getDate()-7);
Form a greaterThan query using that date:
let query = new Parse.Query("Order");
query.greaterThan('createdAt', d);
query.find.then(function(results) {
// results will be instances of Order created starting 7 days ago
// underscore _.groupBy() is useful for grouping
});
Notes:
createdAt to any valid date property on the Order object or leave it as-is if you're going for creation dated