Operation and maintenance monitoring API
Operation and maintenance monitoring methods are used to check database health status, obtain version information, monitor server performance, etc.
ping()
Check whether the database connection is normal.
Syntax
Return value
- Type:
Promise<boolean> - Note: If the connection is normal,
truewill be returned, otherwisefalsewill be returned.
Example
Usage scenarios
- Health Check: The monitoring system regularly checks whether the database is available
- Container Orchestration: Kubernetes health probe
- Load Balancing: Detect the health status of database nodes
Best Practices
buildInfo()
Get MongoDB version information and build details.
Syntax (buildInfo())
Return value (buildInfo())
- Type:
Promise<Object> - Properties:
version(string): version number, such as "6.0.3"versionArray(Array): Array of version numbers, such as [6, 0, 3] gitVersion(string): Git version hashbits(number): System number of digits (32 or 64)debug(boolean): whether it is Debug versionmaxBsonObjectSize(number): maximum size of BSON object
Example (buildInfo())
Example output:
Usage scenarios (buildInfo())
- Compatibility Check: Check whether the MongoDB version meets the minimum requirements
- Function detection: Determine whether to use specific functions based on the version
- Monitoring Report: Record the database version in the monitoring system
Best Practices (buildInfo())
serverStatus()
Obtain server status information, including number of connections, memory usage, operation statistics, etc.
Syntax (serverStatus())
Parameters
- options (Object, optional):
scale(number): scaling factor, used to adjust the size unit1: Bytes (default)1024: KB1048576: MB
Return value (serverStatus())
- Type:
Promise<Object> - Properties:
connections(Object): Connection informationcurrent(number): current number of connectionsavailable(number): Number of available connectionstotalCreated(number): total number of connections created
mem(Object): Memory usage informationresident(number): resident memory (MB)virtual(number): virtual memory (MB)mapped(number): mapped memory (MB)
opcounters(Object): Operation counterinsert(number): insert operandquery(number): Query operandupdate(number): update operanddelete(number): delete operandgetmore(number): getMore operandcommand(number): command operand
network(Object): Network statisticsbytesIn(number): Number of bytes receivedbytesOut(number): number of bytes sentnumRequests(number): total number of requests
uptime(number): running time (seconds)localTime(Date): local time
version(string): MongoDB versionprocess(string): process type
Example (serverStatus())
Basic usage
Use the scale parameter
Usage scenarios (serverStatus())
- Performance Monitoring: Real-time monitoring of database performance indicators
- Capacity Planning: Analyze connection usage and plan connection pool size
- Troubleshooting: Check memory usage, number of connections and other abnormalities
- Alarm System: Set thresholds to trigger alarms
Best Practices (serverStatus())
stats()
Get the statistics of the current database.
Syntax (stats())
Parameters (stats())
- options (Object, optional):
scale(number): scaling factor1: Bytes (default)1024: KB1048576: MB
Return value (stats())
- Type:
Promise<Object> - Properties:
db(string): database namecollections(number): number of setsviews(number): number of viewsobjects(number): total number of documentsavgObjSize(number): average document sizedataSize(number): data sizestorageSize(number): storage sizeindexes(number): index numberindexSize(number): index sizetotalSize(number): total sizescaleFactor(number): scaling factor
Example (stats())
Basic usage (example (stats()))
Use different units
Usage scenarios (stats())
- Capacity Planning: Assess database storage needs
- Performance Optimization: Analyze index space occupied
- Cost Estimation: Calculate storage costs
- Monitoring Alarm: Alarm when the database size exceeds the threshold
Best Practices (stats())
Related documents
- Database Operation - listDatabases, dropDatabase
- Collection Management - Collection statistics and management
- Collection Management Example - Current TypeScript example