ping (Connectivity Check)
Last updated: 2026-10-09
ping (Connectivity Check)
ping verifies the MongoDB server is reachable and responsive, returning { ok: 1 } on success. It is the simplest health-check command, used to verify connections at script startup or as a monitoring probe. Equivalent to db.adminCommand({ ping: 1 }).
| Category | Monitoring & Diagnostics |
|---|---|
| MongoDB Version | 1.0+ |
| Example Language | mongosh (Shell) |
📝 Syntax
db.adminCommand({ ping: 1 })
▶ Example
Run the commands below in your local mongosh (requires a running mongod):
// ping: connectivity self-check
const res = db.adminCommand({ ping: 1 })
if (res.ok === 1) {
print("MongoDB connection OK")
} else {
print("Connection failed: " + res.errmsg)
}
Output:
MongoDB connection OK
📌 Notes
- { ok: 1 } means the connection is healthy
- Works against replica sets/sharded clusters (any reachable member)
- ping performs no data I/O; negligible cost
🏷️ Related Tags
💡 Related Cases
📚 Related Tutorials
❓ FAQ
QCan ping measure latency?
ANot directly. Time db.runCommand({ ping: 1 }) yourself, or use the driver's server-monitoring API.
QWhat does ping return?
AOn success it returns { ok: 1 }; unreachable servers raise a network error or return ok: 0.
Qping vs hello?
Aping only checks reachability; hello reports server role, version, and topology.