How to stop MongoDB in a single command?

To stop MongoDB in a single command, use the shutdown command with the --eval option to execute the shutdown operation directly from the command line without entering the MongoDB shell.

Syntax

mongo --eval "db.getSiblingDB('admin').shutdownServer()"

Example: Stopping MongoDB Server

Execute the shutdown command from your system's command prompt or terminal ?

mongo --eval "db.getSiblingDB('admin').shutdownServer()"

The output shows the MongoDB server shutting down ?

MongoDB shell version v4.0.5
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("c0337c02-7ee2-45d9-9349-b22d6b1ffe85") }
MongoDB server version: 4.0.5
server should be down...
2019-03-14T21:56:10.327+0530 I NETWORK [js] trying reconnect to 127.0.0.1:27017 failed
2019-03-14T21:56:11.331+0530 I NETWORK [js] reconnect 127.0.0.1:27017 failed failed

Alternative Methods

Method 1: Using Ctrl + C

If you have direct access to the MongoDB process, use the keyboard shortcut ?

Ctrl + C

Method 2: Starting MongoDB Again

To restart the MongoDB server after shutdown ?

mongod

How It Works

  • The --eval option executes JavaScript code directly without entering the shell
  • getSiblingDB('admin') switches to the admin database
  • shutdownServer() sends the shutdown command to the MongoDB server
  • The connection errors in the output confirm successful shutdown

Conclusion

Use mongo --eval "db.getSiblingDB('admin').shutdownServer()" to stop MongoDB instantly from the command line. This method is faster than entering the shell and executing shutdown commands manually.

Updated on: 2026-03-15T00:08:48+05:30

722 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements