Database operation API
Database-level management operations, including listing databases, deleting databases, listing collections, etc.
listDatabases()
List all databases on the MongoDB server.
Syntax
Parameters
- options (Object, optional):
nameOnly(boolean): only returns the database name, defaultfalse
Return value
- Do not use nameOnly:
Promise<Array<Object>>name(string): database namesizeOnDisk(number): Disk usage (bytes)empty(boolean): whether it is empty
- Use nameOnly:
Promise<Array<string>>
Example
dropDatabase()
⚠️ Dangerous operation: delete the entire database and cannot recover!
Security mechanism
- Must be explicitly confirmed:
{ confirm: true }must be passed in - Production environment protection:
NODE_ENV=production,prod, orliveis blocked by default and requires{ allowProduction: true } - Audit Log: All deletion attempts will be logged
Syntax (dropDatabase())
Parameters (dropDatabase())
- databaseName (string, required): database name
- options (Object, required):
confirm(boolean, required): must betrueallowProduction(boolean): Whether to allow execution in the production environment, defaultfalseuser(string): operating user (for auditing)
Return value (dropDatabase())
- Type:
Promise<Object> - Properties:
dropped(boolean): Whether the deletion was successfuldatabase(string): deleted database nametimestamp(Date): deletion time
Example (dropDatabase())
❌ Error: No confirmation provided
✅ Correct: Provide confirmation
⚠️ Production environment: additional confirmation required
Error handling
listCollections()
List all collections in the current database.
Syntax (listCollections())
Parameters (listCollections())
- filter (Object, optional): MongoDB collection-list filter
- options (Object, optional): MongoDB listCollections options
Return value (listCollections())
Promise<Array<Object>>
Example (listCollections())
runCommand()
Execute any MongoDB command.
Syntax (runCommand())
Parameters (runCommand())
- command (Object, required): MongoDB command object
- options (Object, optional): command options
Example (runCommand())
Related documents
- Operation and Maintenance Monitoring - ping, buildInfo, serverStatus, stats
- Collection Management - Collection level operations
- Collection Management Example