PyMongo - db.coll.CopyTo

1,188 views
Skip to first unread message

marc.m...@upf.edu

unread,
Apr 9, 2014, 11:19:45 AM4/9/14
to mongod...@googlegroups.com
Hi,

I would like to know if there is an equivalent or workaround for the command

db.collection.CopyTo("newcollection")

in PyMongo.

Thanks

Bernie Hackett

unread,
Apr 9, 2014, 11:29:28 AM4/9/14
to mongod...@googlegroups.com
No, but you could easily write the same thing using PyMongo. You can see how any function in the mongo shell is implemented through it name. From the mongo shell:

> db.foo.copyTo

function ( newName ){

    return this.getDB().eval(

        function( collName , newName ){

            var from = db[collName];

            var to = db[newName];

            to.ensureIndex( { _id : 1 } );

            var count = 0;


            var cursor = from.find();

            while ( cursor.hasNext() ){

                var o = cursor.next();

                count++;

                to.save( o );

            }


            return count;

        } , this.getName() , newName

    );

}



--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at http://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/47057cd4-0cbd-4cb8-80b0-1a84b6b94049%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Paulie Pena

unread,
Jun 4, 2014, 7:01:34 PM6/4/14
to mongod...@googlegroups.com
Since MongoDB just uses eval() within the copyTo() call, you could also just use eval() in pymongo, e.g.
db.eval('db.collection.copyTo("newcollection")')

Stephen Steneker

unread,
Jun 16, 2014, 1:44:01 AM6/16/14
to mongod...@googlegroups.com
Hi Paulie,

Your suggestion won't work.. a client-side eval() in the mongo shell is very different from running the server side eval() via Python. For example, there is no access to a "db" object within a server-side eval().

To mimic the mongo shell's db.collection.copyTo() via PyMongo, you will have to implement the same client-side logic as suggested by Bernie.

Regards,
Stephen

Paulie Pena

unread,
Jun 16, 2014, 9:34:36 AM6/16/14
to mongod...@googlegroups.com
I've tried it using MongoDB version 2.2.2 and pymongo 2.6.3, and it works fine for me.

-Paulie

Paulie Pena

unread,
Jul 25, 2014, 8:52:27 AM7/25/14
to mongod...@googlegroups.com
I just realized that db.eval() creates a global lock which prevents reads and writes to the server, so copyTo() is probably not the safest thing to use, both because Python would need to use db.eval() to call it but also because copyTo() itself calls db.eval() (unless you really want to stop all reads and writes while the collection is copying). Instead of copyTo() and db.eval(), you can prevent the global read/write lock by using db.runCommand({eval:..., nolock:true}), which in Python would be like this (see http://api.mongodb.org/python/2.2/api/pymongo/database.html#pymongo.database.Database.command):
db.command('eval', str_javascript_func, nolock=True)
Reply all
Reply to author
Forward
0 new messages