Bird
Raised Fist0
MongoDBquery~20 mins

Mongos router behavior in MongoDB - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Mongos Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
2:00remaining
How does mongos route a query to shards?

Consider a sharded MongoDB cluster with a mongos router. When a query is sent to mongos, how does it decide which shards to query?

AIt broadcasts the query to all shards regardless of shard key.
BIt uses the shard key in the query to route the query only to relevant shards.
CIt sends the query only to the primary shard of the database.
DIt randomly selects one shard to send the query to.
Attempts:
2 left
💡 Hint

Think about how sharding improves query efficiency by targeting specific shards.

🧠 Conceptual
intermediate
2:00remaining
What role does mongos play in a sharded cluster?

In a MongoDB sharded cluster, what is the primary role of the mongos process?

AIt acts as a query router and interface between clients and shards.
BIt stores the actual data for the shards.
CIt manages the config servers and metadata storage.
DIt performs backups of the cluster data.
Attempts:
2 left
💡 Hint

Think about how clients interact with a sharded cluster.

📝 Syntax
advanced
2:00remaining
Identify the correct mongos query to find documents by shard key

Given a sharded collection with shard key { userId: 1 }, which mongos query will efficiently find documents for userId 123?

MongoDB
db.orders.find({ userId: 123 })
Adb.orders.find({ userId: 123 })
Bdb.orders.find({ _id: 123 })
Cdb.orders.find({ userId: { $gt: 100 } })
Ddb.orders.find({ customerId: 123 })
Attempts:
2 left
💡 Hint

Use the shard key field exactly in the query to target shards.

❓ optimization
advanced
2:00remaining
How to optimize queries with mongos in a sharded cluster?

You notice that queries sent through mongos are slow because they query all shards. What change can optimize query performance?

ARun queries directly on config servers instead of mongos.
BIncrease the number of shards to distribute load.
CAdd the shard key to the query filter to target specific shards.
DDisable sharding on the collection.
Attempts:
2 left
💡 Hint

Think about how mongos decides which shards to query.

🔧 Debug
expert
3:00remaining
Why does mongos return stale data after chunk migration?

After a chunk migration in a sharded cluster, mongos sometimes returns stale data. What is the most likely cause?

AThe client query is missing the shard key field.
BThe shard key was changed during migration causing data loss.
CConfig servers are offline and cannot update mongos.
Dmongos has cached outdated metadata and has not refreshed it yet.
Attempts:
2 left
💡 Hint

Consider how mongos caches cluster metadata.

Practice

(1/5)
1. What is the primary role of the mongos router in a sharded MongoDB cluster?
easy
A. It stores the actual data for the database.
B. It routes queries to the correct shard based on the shard key.
C. It manages user authentication and roles.
D. It backs up the database automatically.

Solution

  1. Step 1: Understand mongos role

    The mongos acts as a query router in a sharded cluster, directing queries to the right shard.
  2. Step 2: Differentiate from other components

    Data storage is handled by shards, not mongos. Authentication and backups are separate concerns.
  3. Final Answer:

    It routes queries to the correct shard based on the shard key. -> Option B
  4. Quick Check:

    mongos routes queries [OK]
Hint: Remember: mongos routes, shards store data [OK]
Common Mistakes:
  • Thinking mongos stores data
  • Confusing mongos with config servers
  • Assuming mongos handles backups
2. Which of the following is the correct way for an application to connect in a sharded MongoDB setup?
easy
A. Connect to the primary replica set member.
B. Connect only to the config servers.
C. Connect directly to each shard individually.
D. Connect to the mongos router.

Solution

  1. Step 1: Identify connection point in sharded cluster

    Applications connect to mongos which routes queries to shards transparently.
  2. Step 2: Exclude other options

    Direct connection to shards or config servers is not recommended; primary replica set member is for replica sets, not sharding.
  3. Final Answer:

    Connect to the mongos router. -> Option D
  4. Quick Check:

    Apps connect to mongos [OK]
Hint: Always connect apps to mongos, not shards [OK]
Common Mistakes:
  • Connecting directly to shards
  • Using config servers for queries
  • Confusing replica sets with sharding
3. Given a sharded cluster with two shards and a mongos router, what will happen when you run this query through mongos?
db.users.find({ userId: 12345 })
Assume userId is the shard key.
medium
A. mongos returns an error because shard key is missing.
B. mongos sends the query to all shards and merges results.
C. mongos routes the query only to the shard containing userId 12345.
D. mongos sends the query to the config servers.

Solution

  1. Step 1: Check query uses shard key

    The query filters on userId, which is the shard key, so mongos can route precisely.
  2. Step 2: Understand mongos routing behavior

    mongos sends the query only to the shard that holds the chunk for userId 12345, not all shards.
  3. Final Answer:

    mongos routes the query only to the shard containing userId 12345. -> Option C
  4. Quick Check:

    Query with shard key routes to one shard [OK]
Hint: Query with shard key goes to one shard only [OK]
Common Mistakes:
  • Assuming mongos queries all shards always
  • Thinking config servers handle queries
  • Believing shard key is optional for routing
4. You wrote a query to find documents by userId in a sharded cluster but get slow results. You realize userId is not the shard key. What is the likely cause?
medium
A. mongos is routing the query to all shards (scatter-gather).
B. mongos is routing the query to only one shard.
C. mongos is ignoring the query and returning empty results.
D. mongos is connecting directly to config servers.

Solution

  1. Step 1: Identify shard key usage in query

    The query filters on userId, which is not the shard key, so mongos cannot route precisely.
  2. Step 2: Understand mongos scatter-gather behavior

    mongos sends the query to all shards (scatter-gather) causing slower results.
  3. Final Answer:

    mongos is routing the query to all shards (scatter-gather). -> Option A
  4. Quick Check:

    Query without shard key causes scatter-gather [OK]
Hint: Queries without shard key hit all shards, slowing response [OK]
Common Mistakes:
  • Assuming query routes to one shard without shard key
  • Thinking mongos returns empty results on missing shard key
  • Confusing config servers with query routers
5. In a sharded MongoDB cluster, you want to optimize query performance for a collection frequently queried by email. The current shard key is userId. What is the best approach involving mongos behavior?
hard
A. Change the shard key to email so mongos routes queries efficiently.
B. Add an index on email and keep the shard key as userId.
C. Connect directly to shards to bypass mongos routing.
D. Disable sharding on the collection to improve query speed.

Solution

  1. Step 1: Analyze query pattern and shard key

    Queries are frequent on email, but shard key is userId, causing scatter-gather queries via mongos.
  2. Step 2: Optimize shard key for routing

    Changing shard key to email lets mongos route queries directly to relevant shards, improving performance.
  3. Final Answer:

    Change the shard key to email so mongos routes queries efficiently. -> Option A
  4. Quick Check:

    Shard key matches query field for efficient mongos routing [OK]
Hint: Match shard key to frequent query field for fast mongos routing [OK]
Common Mistakes:
  • Thinking adding index alone fixes routing
  • Connecting directly to shards bypasses mongos is not recommended
  • Disabling sharding reduces scalability
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard