Estoy atascado cuando recibo un error como este, he estado buscando una solución en todas partes pero no he obtenido ningún resultado. Por favor, ayúdame. gracias
Product.js "Rutas"
router.get("/products/total/", productsCount)Producto.js "Controlador"
exports.productsCount = async (req, res) => { let total = await Product.find({}).limit(0).estimatedDocumentCount().exec(); res.json(total); }Mi error:
Una mejor solución es usar "countDocuments" para obtener el conteo,
Puedes usar esto:
const count = await Product.countDocuments(query);Ejemplos:
const count = await Product.countDocuments({ categoryId : 'TEST' });O
Product.countDocuments({ categoryId : 'TEST' }, function (err, count) { console.log('Products count = ', count); });Puede usar la siguiente agregación en mongodb 3.6
const result = await Product.aggregate([ { "$facet": { "totalData": [ { "$match": { categoryId : 'TEST' }} ], "totalCount": [ { "$count": "count" } ] }} ])con salto y límite
const result = await Product.aggregate([ { "$facet": { "totalData": [ { "$match": { categoryId : 'TEST' }}, { "$skip": 10 }, { "$limit": 10 } ], "totalCount": [ { "$count": "count" } ] }} ])