0
# Administrative Operations
1
2
Database administration capabilities including user management, indexing, and server statistics.
3
4
## Capabilities
5
6
### Admin Instance Creation
7
8
Access the admin interface through the database instance.
9
10
```javascript { .api }
11
/**
12
* Get admin interface for database operations
13
* @returns {SkinAdmin} Admin instance
14
*/
15
db.admin();
16
```
17
18
### Database Management
19
20
Core database administrative operations.
21
22
```javascript { .api }
23
/**
24
* List all databases on the server
25
* @param {function} callback - Callback function (err, result)
26
* @returns {void}
27
*/
28
listDatabases(callback);
29
30
/**
31
* Get server status and statistics
32
* @param {function} callback - Callback function (err, result)
33
* @returns {void}
34
*/
35
serverStatus(callback);
36
37
/**
38
* Get server information
39
* @param {function} callback - Callback function (err, result)
40
* @returns {void}
41
*/
42
serverInfo(callback);
43
44
/**
45
* Ping the database server
46
* @param {function} callback - Callback function (err, result)
47
* @returns {void}
48
*/
49
ping(callback);
50
```
51
52
**Usage Examples:**
53
54
```javascript
55
const mongoskin = require('mongoskin');
56
const db = mongoskin.db('mongodb://localhost:27017/myapp');
57
const admin = db.admin();
58
59
// List all databases
60
admin.listDatabases((err, result) => {
61
if (err) throw err;
62
console.log('Databases:');
63
result.databases.forEach(db => {
64
console.log(`- ${db.name} (${db.sizeOnDisk} bytes)`);
65
});
66
});
67
68
// Get server status
69
admin.serverStatus((err, status) => {
70
if (err) throw err;
71
console.log('Server uptime:', status.uptime);
72
console.log('Current connections:', status.connections.current);
73
console.log('Memory usage:', status.mem);
74
});
75
76
// Get server information
77
admin.serverInfo((err, info) => {
78
if (err) throw err;
79
console.log('MongoDB version:', info.version);
80
console.log('Platform:', info.platform);
81
});
82
83
// Ping server
84
admin.ping((err, result) => {
85
if (err) throw err;
86
console.log('Server ping successful:', result);
87
});
88
```
89
90
### User Management
91
92
User and authentication management operations.
93
94
```javascript { .api }
95
/**
96
* Add user to database
97
* @param {string} username - Username
98
* @param {string} password - Password
99
* @param {object} [options] - User options
100
* @param {function} callback - Callback function (err, result)
101
* @returns {void}
102
*/
103
addUser(username, password, options, callback);
104
105
/**
106
* Remove user from database
107
* @param {string} username - Username to remove
108
* @param {function} callback - Callback function (err, result)
109
* @returns {void}
110
*/
111
removeUser(username, callback);
112
113
/**
114
* Authenticate user credentials
115
* @param {string} username - Username
116
* @param {string} password - Password
117
* @param {function} callback - Callback function (err, result)
118
* @returns {void}
119
*/
120
authenticate(username, password, callback);
121
122
/**
123
* Logout current user
124
* @param {function} callback - Callback function (err, result)
125
* @returns {void}
126
*/
127
logout(callback);
128
```
129
130
**Usage Examples:**
131
132
```javascript
133
const admin = db.admin();
134
135
// Add new user
136
admin.addUser('newuser', 'password123', {
137
roles: ['readWrite']
138
}, (err, result) => {
139
if (err) throw err;
140
console.log('User added:', result);
141
});
142
143
// Authenticate user
144
admin.authenticate('username', 'password', (err, result) => {
145
if (err) throw err;
146
console.log('Authentication successful:', result);
147
});
148
149
// Remove user
150
admin.removeUser('olduser', (err, result) => {
151
if (err) throw err;
152
console.log('User removed:', result);
153
});
154
155
// Logout
156
admin.logout((err, result) => {
157
if (err) throw err;
158
console.log('Logged out:', result);
159
});
160
```
161
162
### Replication Management
163
164
Operations for managing MongoDB replica sets.
165
166
```javascript { .api }
167
/**
168
* Get replica set status
169
* @param {function} callback - Callback function (err, result)
170
* @returns {void}
171
*/
172
replSetGetStatus(callback);
173
174
/**
175
* Initialize replica set
176
* @param {object} config - Replica set configuration
177
* @param {function} callback - Callback function (err, result)
178
* @returns {void}
179
*/
180
replSetInitiate(config, callback);
181
182
/**
183
* Reconfigure replica set
184
* @param {object} config - New replica set configuration
185
* @param {function} callback - Callback function (err, result)
186
* @returns {void}
187
*/
188
replSetReconfig(config, callback);
189
```
190
191
**Usage Examples:**
192
193
```javascript
194
const admin = db.admin();
195
196
// Get replica set status
197
admin.replSetGetStatus((err, status) => {
198
if (err) throw err;
199
console.log('Replica set status:', status.set);
200
console.log('Members:');
201
status.members.forEach(member => {
202
console.log(`- ${member.name}: ${member.stateStr}`);
203
});
204
});
205
206
// Initialize replica set
207
const config = {
208
_id: 'rs0',
209
members: [
210
{ _id: 0, host: 'localhost:27017' },
211
{ _id: 1, host: 'localhost:27018' },
212
{ _id: 2, host: 'localhost:27019' }
213
]
214
};
215
216
admin.replSetInitiate(config, (err, result) => {
217
if (err) throw err;
218
console.log('Replica set initialized:', result);
219
});
220
```
221
222
### Sharding Operations
223
224
MongoDB sharding cluster management.
225
226
```javascript { .api }
227
/**
228
* Enable sharding for database
229
* @param {string} database - Database name
230
* @param {function} callback - Callback function (err, result)
231
* @returns {void}
232
*/
233
enableSharding(database, callback);
234
235
/**
236
* Shard collection
237
* @param {string} collection - Full collection name (db.collection)
238
* @param {object} key - Shard key
239
* @param {function} callback - Callback function (err, result)
240
* @returns {void}
241
*/
242
shardCollection(collection, key, callback);
243
244
/**
245
* Get sharding status
246
* @param {function} callback - Callback function (err, result)
247
* @returns {void}
248
*/
249
printShardingStatus(callback);
250
```
251
252
**Usage Examples:**
253
254
```javascript
255
const admin = db.admin();
256
257
// Enable sharding for database
258
admin.enableSharding('myapp', (err, result) => {
259
if (err) throw err;
260
console.log('Sharding enabled:', result);
261
});
262
263
// Shard a collection
264
admin.shardCollection('myapp.users', { _id: 1 }, (err, result) => {
265
if (err) throw err;
266
console.log('Collection sharded:', result);
267
});
268
269
// Get sharding status
270
admin.printShardingStatus((err, status) => {
271
if (err) throw err;
272
console.log('Sharding status:', status);
273
});
274
```
275
276
### Profiling Operations
277
278
Database profiling and performance monitoring.
279
280
```javascript { .api }
281
/**
282
* Set profiling level
283
* @param {number} level - Profiling level (0=off, 1=slow ops, 2=all ops)
284
* @param {number} [slowms] - Slow operation threshold in milliseconds
285
* @param {function} callback - Callback function (err, result)
286
* @returns {void}
287
*/
288
setProfilingLevel(level, slowms, callback);
289
290
/**
291
* Get current profiling level
292
* @param {function} callback - Callback function (err, result)
293
* @returns {void}
294
*/
295
profilingLevel(callback);
296
297
/**
298
* Get profiling information
299
* @param {function} callback - Callback function (err, result)
300
* @returns {void}
301
*/
302
profilingInfo(callback);
303
```
304
305
**Usage Examples:**
306
307
```javascript
308
const admin = db.admin();
309
310
// Enable profiling for slow operations (>100ms)
311
admin.setProfilingLevel(1, 100, (err, result) => {
312
if (err) throw err;
313
console.log('Profiling enabled for slow ops:', result);
314
});
315
316
// Get current profiling level
317
admin.profilingLevel((err, level) => {
318
if (err) throw err;
319
console.log('Current profiling level:', level);
320
});
321
322
// Get profiling information
323
admin.profilingInfo((err, info) => {
324
if (err) throw err;
325
console.log('Profiling info:', info);
326
});
327
328
// Disable profiling
329
admin.setProfilingLevel(0, (err, result) => {
330
if (err) throw err;
331
console.log('Profiling disabled:', result);
332
});
333
```
334
335
### Index Management
336
337
Global index management operations.
338
339
```javascript { .api }
340
/**
341
* Reindex all collections in database
342
* @param {function} callback - Callback function (err, result)
343
* @returns {void}
344
*/
345
reIndex(callback);
346
347
/**
348
* Validate collection and indexes
349
* @param {string} collection - Collection name
350
* @param {function} callback - Callback function (err, result)
351
* @returns {void}
352
*/
353
validateCollection(collection, callback);
354
```
355
356
**Usage Examples:**
357
358
```javascript
359
const admin = db.admin();
360
361
// Reindex all collections
362
admin.reIndex((err, result) => {
363
if (err) throw err;
364
console.log('Reindex completed:', result);
365
});
366
367
// Validate collection
368
admin.validateCollection('users', (err, result) => {
369
if (err) throw err;
370
console.log('Collection validation:', result);
371
});
372
```
373
374
### Statistics and Metrics
375
376
Database and collection statistics.
377
378
```javascript { .api }
379
/**
380
* Get database statistics
381
* @param {function} callback - Callback function (err, stats)
382
* @returns {void}
383
*/
384
stats(callback);
385
386
/**
387
* Get collection statistics
388
* @param {string} collection - Collection name
389
* @param {function} callback - Callback function (err, stats)
390
* @returns {void}
391
*/
392
collStats(collection, callback);
393
```
394
395
**Usage Examples:**
396
397
```javascript
398
const admin = db.admin();
399
400
// Get database statistics
401
admin.stats((err, stats) => {
402
if (err) throw err;
403
console.log('Database size:', stats.dataSize);
404
console.log('Index size:', stats.indexSize);
405
console.log('Collections:', stats.collections);
406
});
407
408
// Get collection statistics
409
admin.collStats('users', (err, stats) => {
410
if (err) throw err;
411
console.log('Collection documents:', stats.count);
412
console.log('Average document size:', stats.avgObjSize);
413
console.log('Total size:', stats.size);
414
});
415
```