Master MongoDB replication, replica sets, and sharding for distributed deployments. Learn failover, shard keys, and cluster management. Use when setting up high availability or scaling horizontally.
Sets up MongoDB replica sets and sharded clusters with proper failover configuration. Use when configuring high availability or horizontal scaling for MongoDB deployments.
/plugin marketplace add pluginagentmarketplace/custom-plugin-mongodb/plugin install mongodb-developer-plugin@pluginagentmarketplace-mongodbThis skill inherits all available tools. When active, it can use any tool Claude has access to.
assets/config.yamlreferences/GUIDE.mdscripts/helper.pyMaster distributed MongoDB architectures.
# Start mongod instances with replica set config
mongod --replSet rs0 --port 27017
mongod --replSet rs0 --port 27018
mongod --replSet rs0 --port 27019
# Initiate replica set
mongo mongodb://localhost:27017
> rs.initiate({
_id: 'rs0',
members: [
{ _id: 0, host: 'localhost:27017', priority: 1 },
{ _id: 1, host: 'localhost:27018', priority: 0.5 },
{ _id: 2, host: 'localhost:27019', priority: 0 }
]
})
# Check replica set status
> rs.status()
Primary: Accepts reads and writes
Secondary: Replicates from primary, serves reads only
Arbiter: Participates in elections, no data
// Unacknowledged
await collection.insertOne(doc, { writeConcern: { w: 0 } })
// Acknowledged (single node)
await collection.insertOne(doc, { writeConcern: { w: 1 } })
// Majority
await collection.insertOne(doc, { writeConcern: { w: 'majority' } })
// Majority with timeout
await collection.insertOne(doc, {
writeConcern: { w: 'majority', wtimeout: 5000 }
})
// Read from primary only (default)
find().setReadPreference('primary')
// Read from primary, failover to secondary
find().setReadPreference('primaryPreferred')
// Read from secondary if available
find().setReadPreference('secondary')
// Read from secondary, failover to primary
find().setReadPreference('secondaryPreferred')
// Read from nearest node
find().setReadPreference('nearest')
# Start config servers
mongod --configsvr --dbpath /data/config0 --port 27019
mongod --configsvr --dbpath /data/config1 --port 27020
mongod --configsvr --dbpath /data/config2 --port 27021
# Start mongos router
mongos --configdb localhost:27019,localhost:27020,localhost:27021
# Start shard servers
mongod --shardsvr --dbpath /data/shard0 --port 27017
mongod --shardsvr --dbpath /data/shard1 --port 27018
// Enable sharding on database
sh.enableSharding('myapp')
// Shard collection with key
sh.shardCollection('myapp.users', { email: 1 })
// Hash-based sharding (better distribution)
sh.shardCollection('myapp.logs', { userId: 'hashed' })
// Compound shard key
sh.shardCollection('myapp.events', { tenantId: 1, timestamp: 1 })
// Get sharding status
sh.status()
// Get shard information
db.adminCommand({ listShards: 1 })
// Get chunk distribution
sh.balancerStatus()
// Trigger election (step down)
rs.stepDown()
// View election status
rs.status()
// Check oplog
db.oplog.rs.find().sort({ ts: -1 }).limit(5)
// Check replica set members lag
rs.status().members
// Monitor oplog length
db.oplog.rs.find().limit(1).sort({ $natural: -1 })
// Multi-document transaction
const session = client.startSession();
try {
await session.withTransaction(async () => {
await users.insertOne({ name: 'John' }, { session });
await accounts.insertOne({ userId: '...', balance: 100 }, { session });
});
} finally {
await session.endSession();
}
from pymongo import MongoClient
# Connect to replica set
client = MongoClient('mongodb://localhost:27017,localhost:27018,localhost:27019/?replicaSet=rs0')
# Write concern
collection.insert_one(
{'name': 'John'},
write_concern=WriteConcern(w='majority')
)
# Read preference
collection.find().with_options(
read_preference=ReadPreference.SECONDARY
)
✅ Check replica set status regularly ✅ Monitor replication lag ✅ Watch chunk migration progress ✅ Monitor oplog size ✅ Alert on member failures ✅ Track failover events ✅ Monitor balancer activity ✅ Check data distribution
This skill should be used when the user asks to "create a slash command", "add a command", "write a custom command", "define command arguments", "use command frontmatter", "organize commands", "create command with file references", "interactive command", "use AskUserQuestion in command", or needs guidance on slash command structure, YAML frontmatter fields, dynamic arguments, bash execution in commands, user interaction patterns, or command development best practices for Claude Code.
This skill should be used when the user asks to "create an agent", "add an agent", "write a subagent", "agent frontmatter", "when to use description", "agent examples", "agent tools", "agent colors", "autonomous agent", or needs guidance on agent structure, system prompts, triggering conditions, or agent development best practices for Claude Code plugins.
This skill should be used when the user asks to "create a hook", "add a PreToolUse/PostToolUse/Stop hook", "validate tool use", "implement prompt-based hooks", "use ${CLAUDE_PLUGIN_ROOT}", "set up event-driven automation", "block dangerous commands", or mentions hook events (PreToolUse, PostToolUse, Stop, SubagentStop, SessionStart, SessionEnd, UserPromptSubmit, PreCompact, Notification). Provides comprehensive guidance for creating and implementing Claude Code plugin hooks with focus on advanced prompt-based hooks API.