mongodb 在同一数据库中复制集合的最快方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10624964/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the fastest way to copy a collection within the same database?
提问by Tim
I want to copy a collection within the samedatabase and give it a different name - basically take a snapshot.
我想在同一个数据库中复制一个集合并给它一个不同的名字 - 基本上是拍一个快照。
What's the best way to do this? Is there a command, or do I have to copy each record in turn?
做到这一点的最佳方法是什么?有没有命令,还是必须依次复制每条记录?
I'm aware of the cloneCollection
command, but it seems to be for
copying to another server only.
我知道该cloneCollection
命令,但它似乎仅用于复制到另一台服务器。
I'm also aware of mongoimport
and mongoexport
, but as I'm doing this via PHP I'd prefer not to make calls out to the shell.
我也知道mongoimport
and mongoexport
,但是当我通过 PHP 执行此操作时,我不想调用 shell。
采纳答案by AD7six
You have a few options, but the fastest is:
您有几个选择,但最快的是:
mongodump -d db -c sourcecollection
mongorestore -d db -c targetcollection --dir=dump/<db>/<sourcecollection.bson>
or
或者
mongoexport -d db -c sourcecollection | mongoimport -d db -c targetcollection --drop
or in php:
或在 php 中:
`mongoexport -d db -c sourcecollection | mongoimport -d db -c targetcollection --drop`;
after that you have
在那之后你有
mongo db < script.js
where, as shown in the mongo docs, script.js contains something like:
其中,如mongo docs所示,script.js 包含以下内容:
db.myoriginal.find().forEach( function(x){db.mycopy.insert(x)} );
The slowest (by an order of magnitude or more) way to copy a collection will be to use the native php driver - simply because of moving information around. But you could issue the above mongo query if you absolutely want to avoid cli calls using the db executefunction.
复制集合的最慢(一个数量级或更多)方法是使用本机 php 驱动程序 - 仅仅是因为移动信息。但是,如果您绝对想避免使用db execute函数进行cli 调用,则可以发出上述 mongo 查询。
回答by yoooshi
db.myoriginal.aggregate([ { $match: {} }, { $out: "mycopy" } ])
It is a lot faster than doing many inserts in a forEach loop.
它比在 forEach 循环中进行多次插入要快得多。
回答by AbdelHady
Note:Read the answer Updates, they are important!
注意:阅读答案更新,它们很重要!
The most simple & efficient way is by using copyTo(), so you can use:
最简单有效的方法是使用copyTo(),因此您可以使用:
db.source.copyTo("target");
& if "target"
doesn't exist, it will be created
&如果"target"
不存在,它将被创建
-- Update --
- 更新 -
According to CopyTo Documentation, Because copyTo()
uses eval internally, the copy operations will block all other operations on the mongod instance. So it shouldn't be used on productionenvironment.
根据CopyTo Documentation,由于在copyTo()
内部使用 eval,因此复制操作将阻止 mongod 实例上的所有其他操作。所以它不应该用于生产环境。
-- Update --
- 更新 -
Because CopyTo()
uses eval()
internally & eval()
is deprecated since version 3.0, so CopyTo()
is also deprecated since version 3.0.
因为在内部CopyTo()
使用eval()
&eval()
自 3.0 版起已弃用,所以CopyTo()
自 3.0 版起也不推荐使用。
回答by Isura Amarasinghe
First option (Using mongo dump)
第一个选项(使用 mongo dump)
Get a dump from collection
mongodump -d db -c source_collection
Restore from collection
mongorestore -d db -c target_collection dir=dump/db_name/source_collection.bson
从集合中获取转储
mongodump -d db -c source_collection
从集合中恢复
mongorestore -d db -c target_collection dir=dump/db_name/source_collection.bson
Second Option
第二种选择
Running aggregate
db.getCollection('source_collection').aggregate([ { $match: {"emailAddress" : "[email protected]"} }, { $out: "target_collection" } ])
运行聚合
db.getCollection('source_collection').aggregate([ { $match: {"emailAddress" : "[email protected]"} }, { $out: "target_collection" } ])
Third Option (Slowest)
第三个选项(最慢)
Running a through for loop
db.getCollection('source_collection').find().forEach(function(docs){ db.getCollection('target_collection').insert(docs); }) print("Rolleback Completed!");
运行通过 for 循环
db.getCollection('source_collection').find().forEach(function(docs){ db.getCollection('target_collection').insert(docs); }) print("回滚完成!");
回答by Sagar Gupta
This is my implementation in python (pymongo):
这是我在 python (pymongo) 中的实现:
def copy_collection(client, from_db, from_coll, to_db=None, to_coll=None):
to_db = from_db if to_db is None else to_db
to_coll = from_coll if to_coll is None else to_coll
assert (to_db != from_db or to_coll != from_coll), "Copy Error: Source and destination can't be same!"
documents = client[from_db][from_coll].find()
client[to_db][to_coll].insert_many([d for d in documents])
回答by kij
In addition to AD7six 1st solution, if you use mongoexport / import be sure about your collection data types and mongo configuration, as explained here: http://docs.mongodb.org/manual/reference/mongodb-extended-json/
除了 AD7six 1st 解决方案,如果您使用 mongoexport/import,请确保您的集合数据类型和 mongo 配置,如下所述:http: //docs.mongodb.org/manual/reference/mongodb-extended-json/
回答by YongHao Hu
The fastest way is db.collection.copyTo().
最快的方法是db.collection.copyTo()。
Note that it is deprecated since version 3.0.
请注意,它自 3.0 版起已弃用。
回答by Jp Lorandi
You can use the copyDatabase function in the mongo shell:
您可以在 mongo shell 中使用 copyDatabase 函数:
http://docs.mongodb.org/manual/tutorial/copy-databases-between-instances/
http://docs.mongodb.org/manual/tutorial/copy-databases-between-instances/