Java script to use in windows power shell to connect to MongoDB and check status daily

Hi Team,

Can we run java script in windows power shell to connect to MongoDB and check status of the server daily.
Could you please help me

Thanks in Advance

1 Like

Hi @venkata_reddy,

This seems like a possible duplicate of your other question: Health script from Ops manager.

You could certainly write a script to connect to your deployment, although it would be more typical to configure alerts based on conditions of interest or concern using MongoDB Cloud/Ops Manager or a similar management platform.

What status are you looking to check, and what type of deployment do you have (standalone, replica set, or sharded cluster)?


Hi @Stennie,

Thank you for the update.

I am trying to execute certain batch of commands in power shell by passing java script as an argument.

It would look like this:

I created a powershell(test.ps1) script with the following lines in it.

cd “C:\Program Files\MongoDB\Server\4.2\bin”
.\mongod --version
.\mongo --authenticationMechanism=SCRAM-SHA-256 --authenticationDatabase=‘admin’ --username=‘sysdb’ --password=mongo C:\Users\xyz\Desktop\test1.js

where test1.js contains the following:

print(’*** MongoDB Uptime in Days: ’ + db.serverStatus().uptime / 86400 + ’ ***’);

print(’*** MongoDB Version: ’ + db.version() + ’ ***’);

print(’*** replicaset members: ’ + rs.status().members + ’ ***’);



PS C:\Program Files\MongoDB\Server\4.0\bin> C:\Users\xyz\Desktop\test.ps1
db version v4.0.14
git version: 1622021384533dade8b3c89ed3ecd80e1142c132
allocator: tcmalloc
modules: enterprise
build environment:
distmod: windows-64
distarch: x86_64
target_arch: x86_64
MongoDB shell version v4.0.14
connecting to: mongodb://
Implicit session: session { “id” : UUID(“8591efaf-b36c-414c-9c4c-e3301ee32cd3”) }
MongoDB server version: 4.0.14
*** MongoDB Uptime in Days: 5.000949074074074 ***
*** MongoDB Version: 4.0.14 ***
*** replica set members: [object BSON],[object BSON],[object BSON] ***

whether it is text file or java script file as argument
It is not displaying output of a command if it returns more than a line.
Moreover it is not executing the commands placed outside the print() function.
Could you please help me.

Thanks in Advance

1 Like

Hi @venkata_reddy,

There are some differences in interactive vs scripted mongo shell interaction. For more information see: Write Scripts for the mongo Shell.

There’s a particular note which might help with your current approach:

In interactive mode, mongo prints the results of operations including the content of all cursors. In scripts, either use the JavaScript print() function or the mongo specific printjson() function which returns formatted JSON.

This is the expected output of print(rs.status().members). The print() function does not attempt to convert objects to JSON.

Use printjson(rs.status().members) to output in JSON.

The commands are executing, but won’t display any output in scripted mode unless you explicitly call print() or printjson() on a command or its result.



Hi @Stennie,

Great stuff. Thanks for the clear explanation.

Happy New Year in advance !!!

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.