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
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
I’m guessing
cloneCollectionis (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.