What's the fastest way to copy a collection within the same database? Ask Question

What's the fastest way to copy a collection within the same database? Ask Question

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.

ベストアンサー1

> use yourDatabaseName
> db.myOriginal.aggregate([{$out: "myCopy"}])

It is a lot faster than doing many inserts in a forEach loop.

おすすめ記事