简体   繁体   中英

Call stored procedure/function with PHP in Mongo DB sharded cluster

I am using MongoDB 2.6 with two shard clusters config. I want to call a function dataStats() that I create and store in MongoDB. This is my PHP script:

$client = new Mongo();
$db = $client->mydata;

$db->system->js->save(array("_id"=>"dataStats", 
   "value"=>new MongoCode("function() { ... }")));

$db->execute("dataStats()");

This code gives me this error:

  'err' => 'Error: can\'t use sharded collection from db.eval',
  'code' => 16722

The reason is $db->execute method is using Mongo db.eval command which is not supported with sharded collections . Is there a workaround for this issue? How can we call a stored procedure in sharded MongoDB from PHP?

There's no workaround. db.eval doesn't work with sharded collections. You should avoid using it if at all possible, anyway.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM