复制一个集合到自己 [英] duplicate a collection into itself
问题描述
如何将mongodb集合复制到自身中,并在新的_id
中解决了_id
中的冲突?
使用此处的答案复制单个文档是不可行的适用于大型收藏.
我想这样做是为了以一种人为的方式增加测试样本的大小,这只是为了测试可伸缩性.所以我想,除了添加新文档,我还可以将集合复制几次以达到目的.
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.
推荐答案
如果您想为测试目的生成合理的数据,以下是使用一些命令行工具的便捷配方:
If you want to generate plausible data for test purposes, here's a handy recipe using some command line tools:
-
mongodb-schema
推断现有集合的概率模式 -
morelikethis
将该架构转换为模板 -
mgeneratejs
根据架构模板生成新文档 -
mongoimport
以便将新文档导入MongoDB
mongoimport
是标准MongoDB命令行工具的一部分;前三个工具可从npm
安装:
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
根据对现有文档的分析来生成1,000个新文档的示例用法:
Sample usage to generate 1,000 new documents based on an analysis of the existing documents:
mongodb-schema本地主机:27017 mydb.mycollection | morelikethis | mgeneratejs -n 1000 | mongoimport -d mydb -c mycollection
mongodb-schema localhost:27017 mydb.mycollection | morelikethis | mgeneratejs -n 1000 | mongoimport -d mydb -c mycollection
如果您还没有任何测试数据(或者更喜欢描述新文档的形状),则可以始终跳过模式分析,并从mgeneratejs
和mongoimport
开始.
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
.
这篇关于复制一个集合到自己的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!