How can I get the total number of documents after using .find() and before applying .sort(), .skip(), and .limit()?
The object passed to .find() would eventually be generated from a query param,so the results for the count would be different each time. Should I be using promises instead of async/ await?
app.get('/', async (req, res) => {
let pageSize = parseInt(req.query.limit) || 250;
let page = parseInt(req.query.page) || 1;
try {
let products = await db.collection('Furniture')
.find({})
.sort({})
.skip((page - 1) * pageSize)
.limit(pageSize)
.toArray();
res.json({products});
} catch (e) { console.log('Error sending products', e); }
});
A count property should be added to the json sent in the response with the total products that fit the find() parameters.