mongodbAfter 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

mongod --dbpath C:\Mongo\data --logpath C:\mongo\logs\server.log --profile 2 --slowms -1

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

tail -f C:\mongo\logs\server.log

With that you’ll see entries like this

Sun May 12 23:42:22.750 [conn96] query PerfTest_1.Users query: { about.author: "Jonathan" }

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.

db.system.profile.find().pretty()

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

Sr Software Engineer at InspectorIT

Jonathan is an out of the box thinker who has over 10 years experience building and supporting web application software and infrastructure. Jon specializes in ASP.NET C#, Javascript and CSS but is willing to learn anything that gets the job done.

Thomas Edison tried and failed nearly 2,000 times to develop the carbonized cotton-thread filament for the incandescent light bulb. And when asked about it, he said “I didn’t fail; I found out 2,000 ways how not to make a light bulb,” but he only needed one way to make it work.

Facebook Twitter LinkedIn