MongoDB: How to count documents based on a condition (with examples)

Updated: February 3, 2024 By: Guest Contributor Post a comment

Overview

MongoDB, a widely preferred NoSQL database, due to its scalability and flexibility, is an excellent choice for storing and retrieving data in the form of documents. One common requirement while working with databases is to count the number of documents that meet certain criteria. In MongoDB, this can be done using both the count() and aggregate() methods. Through this tutorial, we will explore the various ways one can count documents based on a condition in MongoDB, giving you the nitty-gritty details needed to effectively utilize this powerful functionality.

Getting Started with Counting Documents

Assuming you have already set up your MongoDB instance and have some data to work with, let’s start with the most basic form of counting documents.

db.collection.countDocuments({})

The countDocuments() function, as shown above, will return the total number of documents in the collection when called with an empty query object.

Basic Conditional Counting

Now, to count documents that meet a specific condition, you can pass a query object as an argument to the countDocuments() function.

db.collection.countDocuments({"status": "A"})

If your collection contains documents with a field named status and you’re interested in counting how many of these documents have a status of ‘A’, the above line of code will give you exactly that number.

Using Comparison Operators

MongoDB provides a variety of comparison operators that can be used to refine your query conditions. Suppose you have a products collection and you want to count the number of products with a price less than 100. You would use the $lt (less than) operator as follows:

db.products.countDocuments({"price": {"$lt": 100}})

This returns the count of all products priced below 100. Similarly, you can use $lte (less than or equal to), $gt (greater than), $gte (greater than or equal to), and other comparison operators.

Counting with Logical Operators

Logical operators add even more control over the conditions. With the $or operator, you can count documents that satisfy any of the given conditions. Consider a users collection where you’d like to count users who are either 18 years old or have a verified status of true.

db.users.countDocuments({"$or": [{"age": 18}, {"verified": true}]})

In addition to $or, logical operators like $and, $not, and $nor are also at your disposal for more complex conditional counts.

Advanced Conditional Counts Using Aggregate

For more sophisticated queries and data processing, the aggregate() method becomes invaluable. Say we want to count the number of sales documents that have a total amount greater than 100, after applying a discount.

db.sales.aggregate([
    { "$match": { "status": "A" } },
    { "$group": {
        "_id": null,
        "count": { "$sum": 1 }
    }}
])

In the sales collection, `$match` filters documents where `status` is ‘A’, and `$group` is aggregating the documents to give a count of documents per condition.

Count Documents with Conditions on Array Fields

If your data model includes array fields, you might want to count documents based on elements within these arrays satisfying a condition. The following query counts the number of books where the `tags` array field contains ‘fiction’.

db.books.countDocuments({"tags": "fiction"})

You could also use $elemMatch if you require to match more than one condition within the array field.

Count with Regular Expressions

For pattern matching, MongoDB supports regular expressions allowing for partial matching of string fields. Say we want to count all users with email addresses ending in ‘example.com’.

db.users.countDocuments({"email": /example.com$/ })

With the regex `/example.com$/`, the query would match any documents where the `email` field ends with ‘example.com’.

Conclusion

The ability to count documents conditionally in MongoDB is crucial for generating insights and conducting analysis on data. By utilizing MongoDB’s built-in methods like countDocuments() and aggregate(), you can execute a wide range of counting operations, tailored to complex conditions using various query and aggregation operations. With practice, these commands become essential tools in your data manipulation arsenal.