Live profile your MongoDB server

mongodb server

After scouring the internet to find a SQL Profile like experience for MongoDB and coming up empty I decided I would share my crude way of monitoring the operations of my MongoDB database.  While this won’t work for you if you’re using MongoLab. You can utilize this in your own personal MongoDB server.

I find that there is a massive amount of value to had by simply watching the queries that come into your database to discover patterns and identify possible concerns. I hope that this will become better in the future.

Step One

Enable MongoDB profiling and set the values to show all queries and any query that returns in less than 1ms

[code lang=”bash”]
mongod –dbpath C:Mongodata –logpath C:mongologsserver.log –profile 2 –slowms -1
[/code]

Profile 2 enables all queries to be logged. Why -1 instead of 0? I’m guessing it’s a bug but if you enter zero it will set it to 1ms and won’t show you any queries that respond in under 1 millisecond.

You may also use db.setProfilingLevel(2,-1) if the server is already running.

Step Two

Use tail to monitor the latest entries added to the bottom of the file. You can use Cygwin or UnxUtils in Windwos to accomplish this

[code lang=”bash”]
tail -f C:mongologsserver.log
[/code]

With that you’ll see entries like this

[code lang=”bash”]
Sun May 12 23:42:22.750 [conn96] query PerfTest_1.Users query: { about.author: “Jonathan” }
[/code]

Inserts / Update Documents

Something to note that while find() queries are listed.  Inserting and updating of documents will be logged but details about the documents contents will not logged.

System.Profile

You can also get a historical log of the activity on a collection utilizing the collection’s system.profile collection.

[code language=”javascript”]
db.system.profile.find().pretty()
[/code]

I look forward to the future where the tooling catches up with the MongoDB server.