I am trying to run some server-side JS in Mongo. Operations I am trying to perform are:
db.dropDatabase(); // removing current database
db.copyDatabase('db_dump', 'db', 'localhost'); // substituting it with a dump
Everything works perfectly nice. When I am storing this as a function:
function () {
db.dropDatabase();
return db.copyDatabase('db_dump', 'db', 'localhost');
}
and executing it, everything is also nice and returns me {"ok" : 1}
But when I try to execute this using php driver:
$db->execute("function(){ db.dropDatabase(); return db.copyDatabase('db_dump', 'db', 'localhost'); }");
I get
{
"retval": {
"errmsg":"exception: can't temprelease nested lock",
"code":10298,
"ok":0
},
"ok":1
}
My first though was that I just need to get out of the lock, so I tried this
$db->command(
array(
'$eval' => "function() { db.dropDatabase(); return db.copyDatabase('db_dump', 'db', 'localhost');}"
),
array(
'nolock'=> true
)
);
Nothing else is using database at this point.
Any ideas how to get rid of this error?
I am using Mongo 2.4.4, PHP 5.3.13 and driver 1.2.10
P.S. Tried this on PHP 5.4.16 and the situation is the same
This command cannot be invoked through the eval command (see this thread for some additional discussion on the matter). Rather than use JS methods, you can invoke the copydb command directly with MongoDB::command(). The following script will copy between two databases on the same server, since I omitted the fromhost option:
$m = new MongoClient();
$m->a->drop();
$m->admin->command([
'copydb' => 1,
'fromdb' => 'b',
'todb' => 'a',
]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With