Introduction
Mongos acts like a traffic controller that directs database requests to the right place in a sharded MongoDB setup.
mongos --configdb <configReplSet>/<host1>:<port1>,<host2>:<port2>,...
// Connect to mongos like a normal MongoDB client
mongo --host <mongos-host> --port <mongos-port>mongos --configdb configReplSet/localhost:27019,localhost:27020,localhost:27021
mongo --host localhost --port 27017// Assume mongos is running on localhost:27017 // Insert a document into a sharded collection use shop // Insert a product db.products.insertOne({ _id: 1, name: "Pen", category: "Stationery" }) // Query the product db.products.find({ _id: 1 })
mongos router in a sharded MongoDB cluster?mongos acts as a query router in a sharded cluster, directing queries to the right shard.mongos. Authentication and backups are separate concerns.mongos which routes queries to shards transparently.mongos router, what will happen when you run this query through mongos? db.users.find({ userId: 12345 }) Assume userId is the shard key.userId, which is the shard key, so mongos can route precisely.userId 12345, not all shards.userId in a sharded cluster but get slow results. You realize userId is not the shard key. What is the likely cause?userId, which is not the shard key, so mongos cannot route precisely.email. The current shard key is userId. What is the best approach involving mongos behavior?email, but shard key is userId, causing scatter-gather queries via mongos.email lets mongos route queries directly to relevant shards, improving performance.email so mongos routes queries efficiently. -> Option APass the test and this pattern moves unknown