Working with Databases and Collections
1. Creating Database
| Action | Command | Notes |
| Switch / lazy create | use mydb | DB exists only after first write |
| Explicit (via insert) | db.users.insertOne({}) | Creates DB + collection |
| From driver | client.db("mydb") | Lazy reference |
2. Listing Databases
| Command | Output |
show dbs | mongosh helper, lists with size |
db.adminCommand({listDatabases: 1}) | Full info |
db.getMongo().getDBNames() | Names only |
listDatabases.filter | Filter by criteria |
3. Dropping Database
| Command | Effect |
use mydb; db.dropDatabase() | Removes DB + all collections |
db.adminCommand({dropDatabase: 1}) | Same, explicit |
Warning: Irreversible. Confirm with db.getName() first.
4. Creating Collection
| Approach | Command |
| Implicit | db.users.insertOne({}) |
| Explicit | db.createCollection("users") |
| With validator | db.createCollection("users", { validator: {...} }) |
| Capped | db.createCollection("logs", { capped: true, size: 1048576 }) |
| Time series | db.createCollection("m", { timeseries: { timeField: "ts" } }) |
5. Listing Collections
| Command | Output |
show collections | Names list |
db.getCollectionNames() | Array of names |
db.getCollectionInfos() | Full info: type, options, idIndex |
db.runCommand({listCollections: 1}) | Raw cursor |
6. Dropping Collection
| Command | Effect |
db.users.drop() | Removes collection + indexes |
db.users.deleteMany({}) | Removes docs only, keeps indexes |
7. Renaming Collection
| Option | Description |
db.users.renameCollection("members") | Same DB |
db.adminCommand({renameCollection: "a.x", to: "b.y"}) | Cross-DB (admin auth) |
dropTarget: true | Replace existing target |
8. Getting Collection Stats
| Field | Meaning |
count | Document count |
size | Uncompressed bytes |
storageSize | On-disk size |
avgObjSize | Average doc size |
nindexes | Index count |
totalIndexSize | Index bytes |
db.users.stats({ scale: 1024 * 1024 }); // sizes in MB
9. Creating Capped Collection
| Option | Required | Description |
| capped: true | yes | Marks as capped |
| size | yes | Max bytes |
| max | no | Max document count |
db.createCollection("auditLog", { capped: true, size: 10485760, max: 5000 });
10. Validating Collection
| Option | Effect |
{validate: "users"} | Basic structural check |
full: true | Deep scan including indexes (slow) |
background: true | Run without exclusive lock (7.0+) |
repair: true | Attempt to fix corruption |
11. Understanding Collection Options
| Option | Purpose |
| validator | Schema validation expression |
| validationLevel | strict | moderate | off |
| validationAction | error | warn |
| collation | Default collation |
| writeConcern | Default write concern |
| storageEngine | Per-engine options |
| expireAfterSeconds | TTL (time series only) |
12. Using Namespace Limits
| Limit | Value |
| Namespace length (db.coll) | 255 bytes |
| DB name length | 64 chars |
| Collection name length | ≤255 chars (incl. db prefix) |
| Forbidden chars (DB) | /\." $ |
| Indexes per collection | 64 |
| Index name length | 127 bytes |