db.cloneCollection() while renaming the cloned collection

553 views
Skip to first unread message

Timothee Cour

unread,
Jan 21, 2017, 12:54:21 AM1/21/17
to mongodb-user

How do I call db.cloneCollection() while renaming the cloned collection to a different name?
EG: supposed I have an existing `users.profile` collection that I don't want to override:

db.cloneCollection('mongodb.example.net:27017', 'users.profile') won't work:

Docs ( https://docs.mongodb.com/manual/reference/method/db.cloneCollection/) says:
> Changed in version 3.0: If the given namespace already exists in the destination mongod instance, db.cloneCollection() will return an error.

I'd like to have something like:

db.cloneCollectionAndRename('mongodb.example.net:27017', 'users.profile', 'users.profile_cloned') 

Is the only workaround to do these 4 steps?

db.profile.renameCollection('users.profile_temp')
db.cloneCollection('mongodb.example.net:27017', 'users.profile')
db.profile.renameCollection('users.profile_cloned')
db.profile_temp.renameCollection('users.profile')



Samantha Ritter

unread,
Jan 24, 2017, 7:07:58 PM1/24/17
to mongodb-user
Hi Timothee,


Yes, unfortunately there is no way to use the cloneCollection command such that your copy has a different collection name than the original collection. The 4-step solution that you present is a good workaround.  However, note that renameCollection() is not compatible with sharded collections, and it will block all database activity during the renaming. See more info here: https://docs.mongodb.com/manual/reference/command/renameCollection/#behavior


There is an open ticket for this feature, though it's not currently slated for any particular release.  Although the requested feature on that ticket is phrased "copyCollection," it would behave like cloneCollection.  If you feel that this feature would be helpful then I'd encourage you to add your vote to it: https://jira.mongodb.org/browse/SERVER-732


Best,
Samantha

Timothee Cour

unread,
Jan 25, 2017, 1:47:28 AM1/25/17
to mongodb-user


On Tuesday, January 24, 2017 at 4:07:58 PM UTC-8, Samantha Ritter wrote:
Hi Timothee,


Yes, unfortunately there is no way to use the cloneCollection command such that your copy has a different collection name than the original collection. The 4-step solution that you present is a good workaround.  However, note that renameCollection() is not compatible with sharded collections, and it will block all database activity during the renaming. See more info here: https://docs.mongodb.com/manual/reference/command/renameCollection/#behavior


There is an open ticket for this feature, though it's not currently slated for any particular release.  Although the requested feature on that ticket is phrased "copyCollection," it would behave like cloneCollection.  If you feel that this feature would be helpful then I'd encourage you to add your vote to it: https://jira.mongodb.org/browse/SERVER-732


done, thanks! Btw another question: I'm guessing `cloneCollection` is (much) more efficient than proposed workaround in https://jira.mongodb.org/browse/SERVER-732?focusedCommentId=419887&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-419887
```
db.<collection_name>.find().forEach(function(d){ db.getSiblingDB('<new_database>')['<collection_name>'].insert(d); });
```

but can't find any reference saying it's indeed more efficient; are there implementation detail notes to confirm this?

Wan Bachtiar

unread,
Jan 30, 2017, 6:25:03 PM1/30/17
to mongodb-user

I’m guessing cloneCollection is (much) more efficient than proposed workaround in …but can’t find any reference saying it’s indeed more efficient; are there implementation detail notes to confirm this?

Hi Timothee,

Yes, cloneCollection is more efficient that the proposed workaround of find().forEach() with insert().

The cloneCollection command copies a collection from a remote mongod instance to the current mongod instance. This command is performed between two mongod servers. The suggested workaround copies collection data from a remote mongod by retrieving documents via find() to the client and then insert() to the current mongod instance.

If you would like to know on the implementation details, you can also review the code for the commands clone_collection.cpp.

Regards,

Wan.

Reply all
Reply to author
Forward
0 new messages