-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsharding-setup.js
More file actions
25 lines (19 loc) · 814 Bytes
/
sharding-setup.js
File metadata and controls
25 lines (19 loc) · 814 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
require('dotenv').config();
const { MongoClient } = require('mongodb');
(async () => {
try {
const client = new MongoClient(process.env.MONGODB_URI, { useNewUrlParser: true, useUnifiedTopology: true });
await client.connect();
const adminDb = client.db('admin');
const dbName = 'advancedDB';
const collectionName = `${dbName}.posts`;
console.log(`Enabling sharding on database "${dbName}"...`);
await adminDb.command({ enableSharding: dbName });
console.log(`Sharding collection "${collectionName}" on key { author: 1 }...`);
await adminDb.command({ shardCollection: collectionName, key: { author: 1 } });
console.log("Sharding configured successfully.");
await client.close();
} catch (err) {
console.error("Error during sharding setup:", err);
}
})();