duplicate a collection into itself

前端 未结 1 1223
野的像风
野的像风 2021-01-25 23:19

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

相关标签:
1条回答
  • 2021-01-25 23:41

    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.

    0 讨论(0)
提交回复
热议问题