I want to copy a collection within the same database and give it a
different name – basically take a snapshot.
What’s the best way to do this? Is there a command, or do I have to
copy each record in turn?
I’m aware of the cloneCollection command, but it seems to be for
copying to another server only.
I’m also aware of mongoimport and mongoexport, but as I’m doing this via PHP I’d prefer not to make calls out to the shell.
You have a few options, but the fastest is:
or
or in php:
after that you have
where, as shown in the mongo docs, script.js contains something like:
The slowest (by an order of magnitude or more) way to copy a collection will be to use the native php driver – simply because of moving information around. But you could issue the above mongo query if you absolutely want to avoid cli calls using the db execute function.