Is it possible to access MongoDB's profile from the command line programatically?
bash, mongodb, php, shell
Solution
In addition to running js files directly or connecting with a driver, you can also directly query mongodb from bash with curl / wget. All you need is to start your server with --rest
mongod --rest
If your server listens to 27017, then add 1000 to get the http port (in this case 28017). The page that you get is the admin page. You can do basic queries here like:
$ curl -i http://localhost:28017/<dbName>/<collectionName>/
i.e.
$ curl http://localhost:28001/test/foo/?filter_str=Hello
{
"offset" : 0,
"rows": [
{ "_id" : { "$oid" : "50ab8a10df015a6dd00a44a7" }, "str" : "Hello" } ,
{ "_id" : { "$oid" : "50ab8a14df015a6dd00a44a8" }, "str" : "Hello", "x" : 1 }
],
"total_rows" : 2 ,
"query" : { "str" : "Hello" } ,
"millis" : 0
}
Problem
I want to create a monitoring script that sends me alerts about slow queries. So I want to get the profile data programatically. I guess I could do it from PHP by connecting to MongoDB (not sure though). But it would be better if I could access the profiler directly from a bash script. Is that possible?