How to count the data in MongoDB using count Method

Now we have attach the file of count function using get method 


router.get(`/get/count`, async (req, res) => {
  try {
    const productCount = await Product.countDocuments();
 
    if (productCount === 0) {
      res.status(404).json({ success: false, message: 'Product not found' });
    }
 
    res.send({ count: productCount });
  } catch (error) {
    res.status(500).json({ success: false, message: 'Internal Server Error' });
  }
});



module.exports = router;

Post a Comment

0 Comments