pymongo : how to copy database ?

1,589 views
Skip to first unread message

Nicolas Clairon

unread,
Mar 6, 2010, 8:37:25 AM3/6/10
to mongod...@googlegroups.com
Hi,

I want to copy a database to another name into the same server with
pymongo. The mongodb documentation says:

> db.copyDatabase(<from_dbname>, <to_dbname>, <from_host>);

but there's no such method on pymongo (no Connection.copy_database).
So I tried with the `command` method without success :

>>> con = pymongo.Connection()
>>> con.test.command({'copydb':True, 'fromdb': 'test', 'todb': 'test2'})
<class 'pymongo.errors.OperationFailure'>: command {'fromdb': 'test',
'todb': 'test2', 'copydb': True} failed: no such cmd

>>> con.test.command({'copydb':True})
<class 'pymongo.errors.OperationFailure'>: command {'copydb': True}
failed: access denied

Any ideas ?

N.

Dwight Merriman

unread,
Mar 6, 2010, 8:54:04 AM3/6/10
to mongod...@googlegroups.com
you can use runCommand to do it.  evaling copyDatabase() in the sheell without the parens will show what is does, hopefully you can figure out the right command from that as i don't know pymongo:

> db.copyDatabase
function (fromdb, todb, fromhost, username, password) {
    assert(isString(fromdb) && fromdb.length);
    assert(isString(todb) && todb.length);
    fromhost = fromhost || "";
    if (username && password) {
        var n = this._adminCommand({copydbgetnonce:1, fromhost:fromhost});
        return this._adminCommand({copydb:1, fromhost:fromhost, fromdb:fromdb, todb:todb, username:username, nonce:n.nonce, key:this.__pwHash(n.nonce, username, password)});
    } else {
        return this._adminCommand({copydb:1, fromhost:fromhost, fromdb:fromdb, todb:todb});
    }
}



--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com.
To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.


Nicolas Clairon

unread,
Mar 6, 2010, 11:12:24 AM3/6/10
to mongod...@googlegroups.com
Ok, I succeed to copy a database via the mongo shell :

> use admin
> db.runCommand({'copydb':1, 'fromdb': 'test', 'todb': 'test2'})

but when I run the same command with pymongo, I doesn't work:

>>> import pymongo
>>>> con = pymongo.Connection()

>>> con.admin.command({'copydb':1, 'fromdb': 'test', 'todb': 'test2'})


<class 'pymongo.errors.OperationFailure'>: command {'fromdb': 'test',

'todb': 'test2', 'copydb': 1} failed: no such cmd

What append ?

# pymongo 1.4
# mongodb 1.3.2

Eliot Horowitz

unread,
Mar 6, 2010, 11:22:56 AM3/6/10
to mongod...@googlegroups.com
I think you need to use SON or something like that in pymongo since
dicts aren't ordered

Nicolas Clairon

unread,
Mar 6, 2010, 11:29:08 AM3/6/10
to mongod...@googlegroups.com
OK, I was just mis-use SON :

>>> from pymongo.son import SON
>>> con.admin.command(SON(copydb=1, fromdb='test', todb='test2'))
<class 'pymongo.errors.OperationFailure'>: command SON([('fromdb',
'test'), ('todb', 'test2'), ('copydb', 1)]) failed: no such cmd

I also tried :

>>> con.admin.command(SON(copydb=1, fromdb='test', todb='test2'))
<class 'pymongo.errors.OperationFailure'>: command SON([('fromdb',
'test'), ('todb', 'test2'), ('copydb', 1)]) failed: no such cmd

Which is weird as arguments are ordered...

Anyway, after digging, I tried :

>>> con.admin.command(SON([("copydb", 1), ("fromdb", 'test'), ("todb", 'test2')]))
{u'ok': 1.0}

Thanks !

Dwight Merriman

unread,
Mar 6, 2010, 12:28:47 PM3/6/10
to mongod...@googlegroups.com, Mike Dirolf
the command name has to be the first BSON field.  i don't know how to do that with PyMongo will wait for Mike to answer.
Reply all
Reply to author
Forward
0 new messages