0
0
MongoDBquery~5 mins

Boolean and null types in MongoDB - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Boolean and null types
O(n)
Understanding Time Complexity

We want to understand how the time it takes to work with Boolean and null types in MongoDB changes as we handle more data.

Specifically, we ask: how does checking or filtering these types grow when the data grows?

Scenario Under Consideration

Analyze the time complexity of the following code snippet.


db.collection.find({
  isActive: true,
  deletedAt: null
})
.limit(10)

This code finds up to 10 documents where the field isActive is true and deletedAt is null.

Identify Repeating Operations

Look for repeated work done by the database when running this query.

  • Primary operation: Scanning documents to check if isActive is true and deletedAt is null.
  • How many times: Once for each document until 10 matches are found or all documents are checked.
How Execution Grows With Input

As the number of documents grows, the database checks more documents to find matches.

Input Size (n)Approx. Operations
10Up to 10 checks
100Up to 100 checks
1000Up to 1000 checks

Pattern observation: The number of checks grows roughly in direct proportion to the number of documents until the limit is reached.

Final Time Complexity

Time Complexity: O(n)

This means the time to find matching documents grows linearly with the number of documents in the collection.

Common Mistake

[X] Wrong: "Checking for Boolean or null fields is instant no matter how many documents there are."

[OK] Correct: The database must look at each document to check these fields unless there is an index, so more documents mean more work.

Interview Connect

Understanding how simple filters on Boolean and null fields scale helps you explain query performance clearly and confidently in real-world situations.

Self-Check

What if we added an index on isActive? How would the time complexity change?