Search code examples
mongodbmongoosediscorddiscord.js

Mongoose - Only count certain documents


I'm currently coding a Discord bot, and I'm trying to count every document within a model based on an object. Simply put, I want to count every document that has ID: 1 and exclude everything else.


Solution

  • You can use mongoose find() method.

    model.find({id: 1}).then(data => console.log(data.length)).catch(err => ...)
    

    The data will be an array of documents with ID = 1 and the log will return number of documents.