Sling Academy
Home/MongoDB/MongoDB: How to count documents based on a condition (with examples)

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

Last updated: February 03, 2024

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.

Next Article: MongoDB: 3 ways to select a random document from a collection

Previous Article: MongoDB: Find documents whose field contains a substring (with examples)

Series: MongoDB Tutorials

MongoDB

You May Also Like

  • MongoDB: How to combine data from 2 collections into one
  • Hashed Indexes in MongoDB: A Practical Guide
  • Partitioning and Sharding in MongoDB: A Practical Guide (with Examples)
  • Geospatial Indexes in MongoDB: How to Speed Up Geospatial Queries
  • Understanding Partial Indexes in MongoDB
  • Exploring Sparse Indexes in MongoDB (with Examples)
  • Using Wildcard Indexes in MongoDB: An In-Depth Guide
  • Matching binary values in MongoDB: A practical guide (with examples)
  • Understanding $slice operator in MongoDB (with examples)
  • Caching in MongoDB: A practical guide (with examples)
  • CannotReuseObject Error: Attempted illegal reuse of a Mongo object in the same process space
  • How to perform cascade deletion in MongoDB (with examples)
  • MongoDB: Using $not and $nor operators to negate a query
  • MongoDB: Find SUM/MIN/MAX/AVG of each group in a collection
  • References (Manual Linking) in MongoDB: A Developer’s Guide (with Examples)
  • MongoDB: How to see all fields in a collection (with examples)
  • Type checking in MongoDB: A practical guide (with examples)
  • How to query an array of subdocuments in MongoDB (with examples)
  • MongoDB: How to compare 2 documents (with examples)