Shraddheya Shendre
Shraddheya Shendre

Reputation: 171

duplicate a collection into itself

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.

Upvotes: 1

Views: 577

Answers (1)

Stennie
Stennie

Reputation: 65323

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 collection
  • morelikethis to convert that schema to a template
  • mgeneratejs to generate new documents according to a schema template
  • mongoimport 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.

Upvotes: 2

Related Questions