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 not feasible for large collections.
I want to do this to increase the size of the testing sample in a contrived manner, this is just for testing the scalability. So I thought, instead of adding new documents, I could duplicate the collection into itself a few times to achieve the purpose.
Asked
Active
Viewed 490 times
1

Shraddheya Shendre
- 171
- 4
- 14
-
Why would you want to "copy" all the content again with different `_id` values in the same collection? Be specific in your question or you will get a wrong answer. – Neil Lunn Jul 03 '17 at 07:32
-
@NeilLunn - I added the reason why I want to do it. – Shraddheya Shendre Jul 03 '17 at 07:39
1 Answers
2
If you want to generate plausible data for test purposes, here's a handy recipe using some command line tools:
mongodb-schema
to infer a probablistic schema for an existing collectionmorelikethis
to convert that schema to a templatemgeneratejs
to generate new documents according to a schema templatemongoimport
to import the new documents into MongoDB
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
.

Stennie
- 63,885
- 14
- 149
- 175