Saturday, March 24, 2012

How to save mongoDB server status into a file

MongoDB serverStatus command provides a useful information about mongod instance. This information includes fields such as mem.virtual, globalLock.currentQueue.writers, extra_info.page_faults. This information can be accessed by executing db.serverStatus() command from the shell.

If you want to save the above result into a file, you can do as follows:

>mongo --eval "printjson(db.serverStatus())" >>{outfile path}

e.g.
>mongo --eval "printjson(db.serverStatus())" >>C:\harinder\log.txt

You can also execute the same using javascript file as an argument. The advantage of this approach is that you can have multiple commands in the file.

>mongo <{Javascript path} >>{Outfile path}

e.g.
>mongo < C:\harinder\mongo.js >>C:\harinder\log.txt

Example of Javascript file:
db.getProfilingLevel()
db.getLastError()
db.foo.find()

Output example:
MongoDB shell version: 2.0.4
connecting to: test
> 0
> null
> { "_id" : ObjectId("4f6e92311c7ef28999436998"), "a" : 1 }
> bye

No comments: