在 MongoDB 中克隆一个集合

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8933307/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 12:25:37  来源:igfitidea点击:

Clone a collection in MongoDB

mongodb

提问by Daryna

I want to clone a MongoDB collection and save it on the same server with a different name. So for example right now I have the following collections: demo1.categories, demo1.users and demo2.users.

我想克隆一个 MongoDB 集合并使用不同的名称将它保存在同一台服务器上。例如,现在我有以下集合:demo1.categories、demo1.users 和 demo2.users。

I want to have a "demo2.categories" which is identical to "demo1.categories". (It just has a different name.)

我想要一个与“demo1.categories”相同的“demo2.categories”。(它只是有一个不同的名字。)

回答by lhagemann

Yet again the MongoDB documentation comes to the rescue

MongoDB 文档再次来拯救

assuming that the collection actually is named "demo1.categories":

假设该集合实际上被命名为“demo1.categories”:

db.demo1.categories.find().forEach( function(x){db.demo2.categories.insert(x)} );

回答by AbdelHady

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 production environment.

根据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.

因为从 3.0 版开始不推荐CopyTo()使用eval()内部使用& eval(),所以CopyTo()从 3.0 版开始也不推荐使用。

回答by Tutankhamen

This is the fastest way to clone your collection:

这是克隆集合的最快方法:

mongoexport -d db_name -c src_collection | mongoimport -d db_name -c dst_collection --drop

it will clone src_collectionin db_nameto dst_collection. Or you can do it in two steps on bson level:

它会克隆src_collectionDB_NAMEdst_collection。或者您可以在 bson 级别分两步完成:

mongodump -d db_name -c src_collection
mongorestore --drop -d db_name -c dst_collection ./dump/db_name/src_collection.bson

回答by tejzpr

The fastest option is

最快的选择是

db.myoriginal.aggregate([ { $out: "mycopy" } ])

回答by blueskin

If you're concerned about speed then I found that by using aggregatewith $projectand $outto be a 100 times faster, not sure if there are restrictions though, but you would have to create a set of fields that you'd want to copy For example:

如果您担心速度,那么我发现通过使用aggregatewith$project$out速度要快 100 倍,但不确定是否有限制,但您必须创建一组要复制的字段,例如:

// Set of fields in the categories collection
var setOfFields = {field1:1, field2:1.......}
db.demo1.categories.aggregate([{ "$project": setOfFields},{ $out: "demo2.categories"}]);

This copies (projects) the selected set of fields for all documents from demo1.categoriesto demo2.categories

这将复制(投影)所有文档的选定字段集,从demo1.categoriesdemo2.categories

回答by kerwin

there already has a command for this.

已经有一个命令。

Copy a single collection from one server to another. http://www.mongodb.org/display/DOCS/cloneCollection+Command

将单个集合从一台服务器复制到另一台服务器。 http://www.mongodb.org/display/DOCS/cloneCollection+Command

回答by Ajay

Don't use db.cloneCollection()method, it is depreciated from the current version 4.2instead try to use mongoexport.

不要使用db.cloneCollection()方法,它从当前版本开始贬值,4.2而是尝试使用mongoexport.

depreciated collection method

折旧征收方式

回答by Ajay

In the mongo console, you can do the following as well, where db_host is the machine where db_host has the db with the collection that you want to clone.

在 mongo 控制台中,您也可以执行以下操作,其中 db_host 是 db_host 具有要克隆的集合的数据库的机器。

use db.cloneCollection(, )

使用 db.cloneCollection(, )