In a way, it's pretty similar to what we can do with MySQL's "RENAME TABLE" command:
In the background, MongoDB will dump and restore it automatically. There is no metadata "magic" for such a rename since databases reside in different files on disk. It just saves a bit of work, and it's worth knowing about.db.runCommand({renameCollection:"sourcedb.mycol",to:"targetdb.mycol"})
Great thanks!
ReplyDeleteshould switch to use admin database before running this command, as described in:
https://jira.mongodb.org/browse/PHP-282
thanks
Deletethanks!!!
ReplyDeleteThanks!
ReplyDeletewhat if sourcedb is on server 1.1.1.1 (with or without some credentials) and targetdb is on 2.2.2.2? How would the command look like?
ReplyDeleteFinally
ReplyDeleteyou'll want to use db.cloneCollection() to copy from someDatabase on 1.1.1.1 to someDatabase on 2.2.2.2, and then if you need the collection in aDifferentDatabase on 2.2.2.2, use the renameCollection command as described above to move it over.
ReplyDeleteIf we use this, and there is existing data in the new database, will it overwrite it or add to it?
ReplyDeleteFor example, myDatabase (original) copy registrations collection to historyDatabase.registrations. If there is already data in historyDatabase.registrations will it be overwritten or appended?