Back to Directory
How to Monitor Recent

How to Monitor MongoDB & Check Cluster Connection Health

SAuthor

How to Monitor MongoDB Uptime & Cluster Health

MongoDB is a popular document database used for storage of unstructured or semi-structured data. If your MongoDB cluster suffers an outage, runs out of disk storage space, or reaches open sockets limits, your application is blocked from saving sessions or loading pages.

Since MongoDB clusters (like MongoDB Atlas) sit behind private security networks, public checking nodes cannot directly connect to them. This guide describes how to expose a secure cluster status endpoint and configure notifications before bottlenecks trigger severe incidents.


Method 1: Set Up an Application-Level Cluster Health Endpoint

To safely verify MongoDB access from the public internet, check cluster ping status inside your Node.js or Python backend application, and expose a protected endpoint.

Node.js (Express & Mongoose) Database Health Route Example

Configure a MongoDB connection health check route:

const express = require('express');
const mongoose = require('mongoose');
const app = express();

// Set database connection options with short timeout parameters
const dbURI = process.env.MONGODB_URI;
const options = {
  serverSelectionTimeoutMS: 2000 // Time out after 2 seconds if DB hangs
};

app.get('/api/healthz/db', async (req, res) => {
  try {
    // Run db.admin().ping() command to check connection
    const db = mongoose.connection.db;
    const result = await db.admin().ping();
    if (result && result.ok === 1) {
      res.status(200).json({ status: 'OK', database: 'connected' });
    } else {
      res.status(500).json({ status: 'ERROR', message: 'MongoDB ping failed' });
    }
  } catch (err) {
    console.error('MongoDB connection check failed:', err);
    res.status(500).json({ status: 'ERROR', message: 'Database offline' });
  }
});

app.listen(3000);

Configure Pingzo to Probe the MongoDB Health Endpoint

  1. Log in to your Pingzo Dashboard.
  2. Create an HTTP/HTTPS Monitor pointing to your endpoint (e.g. https://your-api.com/api/healthz/db).
  3. Set your monitoring check frequency to 1 minute and set up WhatsApp Alerts to page you instantly the second a connection issue is flagged.

Method 2: Push Monitoring (Heartbeat) for MongoDB Backups & Exports

If you run background mongodump cron tasks to back up your collections, use Heartbeat (Push) Monitoring in Pingzo:

  • Configure a Heartbeat Monitor on Pingzo.
  • Add a curl ping at the end of your backup script:
#!/bin/bash
mongodump --uri="mongodb://localhost:27017/db" --out=/backups/ && \
curl -fsS https://ping.pingzoapp.com/ping/your-unique-heartbeat-id

If the database hangs or locks, the dump command fails, the heartbeat is skipped, and Pingzo alerts you.


Key MongoDB Metrics to Monitor

Track these internal metrics in your cluster dashboard:

  • Active Connections: Exceeding database socket limits will block new client handshakes.
  • Replication Lag: In replica sets, monitor secondary replica lag to guarantee read redundancy.
  • Opcounters: Track command operations rates (inserts, queries, updates) to notice traffic spikes.

Summarize with AI

Instantly generate a summary of this page using your favorite LLM

pz-console
// MongoDB Shell
$mongosh --eval "db.adminCommand({ping: 1})"
// Mongoose Ping Check
const mongoose = require('mongoose');
const db = mongoose.connection.db;
const result = await db.admin().ping(); // Returns { ok: 1 }
[pingzo-agent] monitoring active: MongoDB
[pingzo-agent] status: 200 OK | latency: 85ms
DevOps Uptime Check

Uptime monitoring built for MongoDB developers

Configure HTTP health routes, inspect SSL status, and track cron workers from our console dashboard. Get direct texts on WhatsApp.

Start Monitoring Free