Atlas data federation concept in MongoDB - Time & Space Complexity
When using Atlas Data Federation, we combine data from many sources into one query. Understanding time complexity helps us see how query time grows as data size grows across these sources.
We want to know how the work done changes when we ask for more data or connect more sources.
Analyze the time complexity of the following MongoDB query using Atlas Data Federation.
db.getCollection('federatedCollection').find({ status: 'active' }).toArray()
This query fetches all documents with status 'active' from multiple federated sources as if they were one collection.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Scanning each federated source to find matching documents.
- How many times: Once per document in each source that matches the filter.
As the total number of documents across all sources grows, the query work grows roughly in proportion.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 document checks |
| 100 | About 100 document checks |
| 1000 | About 1000 document checks |
Pattern observation: The work grows linearly as more documents are checked across federated sources.
Time Complexity: O(n)
This means the time to run the query grows directly with the total number of documents checked across all sources.
[X] Wrong: "Querying federated data is always instant regardless of data size."
[OK] Correct: Because the query must check documents in each source, more data means more work and longer time.
Knowing how query time grows with data size in federated systems shows you understand real-world data challenges. This skill helps you design better queries and systems.
"What if we added indexes on the federated sources for the 'status' field? How would the time complexity change?"