How to stop mongodb server on Windows?

From the mongo shell documentation:


use dbname

This command does not work in scripted mode. Instead you will need to explicitly define the database in the connection (/dbname in the example above).

Alternately, you can also create a connection within the script:

db2 = connect("server:27017/otherdbname")

I've come up with the following code: Save the following snippet in stop_mongod.js file:

 db = connect("localhost:27017/admin");
 db.shutdownServer();
 quit();

Adjust the connection string if necessary. Then from the command line or within your batch script:

mongo stop_mongod.js

  1. Open Command Prompt/Powershell as Administrator.

  2. Execute

    net stop MongoDB


I'm not sure that's a proper way to do, sending a kill could probably cause damage to your mongo server, and you'll need to repair your database after in case of crash.

Maybe you already solved this question but here is what I do :

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

I'm automatically connected on the admin's collection and next, I just have to run the extinction.

Here is the official link about how to stop Mongodb properly : http://api.mongodb.org/wiki/current/Starting%20and%20Stopping%20Mongo.html

Best