How can I copy a mongodb collection into itself with clashes in _id
resolved by a new _id
?
Copying individual documents using the answer here is no
If you want to generate plausible data for test purposes, here's a handy recipe using some command line tools:
mongoimport
is a part of the standard MongoDB command line tools; the first three tools are installable from npm
:
npm install -g morelikethis mongodb-schema mgeneratejs
Sample usage to generate 1,000 new documents based on an analysis of the existing documents:
mongodb-schema localhost:27017 mydb.mycollection | morelikethis | mgeneratejs -n 1000 | mongoimport -d mydb -c mycollection
If you don't have any test data yet (or prefer to describe the shape of new documents) you could always skip the schema analysis and start with mgeneratejs
and mongoimport
.