开发者

Execute MongoDb Statements in Python

I have a Webserver running in Python. He is getting some Data from some Apps and need to store these in MongoDB. My MongoDB is sharded. Now i want that my Webserver know how much Shards MongoDB开发者_如何学Python has. At the moment he reads this from a cfg file. There is an Statement in MongoDb named printshardingstatus where u can see all shards. So i tried to call this statement from my Pythonserver. But it seems that it is not possible.I dont find such a function in the Pymongo API.

So my question is, is there an chance to run an MongoDB Statement in Python, so that it is directly passed and executed in MongoDB ?


There is no server command for printShardingStatus -- it is just a mongo (javascript) shell helper function. The helper is a few queries against the config database and then some grouping and formatting to make things look nicer.

If you ever want to see how things work in the javascript shell you can just remove the parens ("()") from the function and it will print the javascript code.

Here is the code from the javascript shell.

> printShardingStatus
function (configDB, verbose) {
   if (configDB === undefined) {
       configDB = db.getSisterDB("config");
   }
   var version = configDB.getCollection("version").findOne();
   if (version == null) {
       print("not a shard db!");
       return;
   }
   var raw = "";
   var output = function (s) {raw += s + "\n";};
   output("--- Sharding Status --- ");
   output("  sharding version: " +
   ...


Have you tried pymongo or mongoengine? Then perhaps send a message to the authors...


You can simply get config databasr and execute find() on shards collection just like normal collection.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜