mongodb mongo 从一个集合复制到另一个集合(在同一个数据库上)

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

mongo copy from one collection to another (on the same db)

mongodb

提问by Marcin Wasiluk

I have got mongo db called testand in this db two collections collection1and collection1_backup. How to replace content of collection1with data from collection1_backup.

我已经调用了 mongo db test,在这个 db 中有两个集合collection1collection1_backup. 如何collection1用来自collection1_backup.

回答by Sammaye

The best way to have done this (considering the name of the collection ends with _backup) is possibly to have used mongorestore: http://docs.mongodb.org/manual/reference/mongorestore/

完成此操作的最佳方法(考虑到以 结尾的集合名称_backup)可能是使用 mongorestore:http://docs.mongodb.org/manual/reference/mongorestore/

However in this case it depends. If the collection is unsharded you can use renameCollection( http://docs.mongodb.org/manual/reference/command/renameCollection/) or you can use a more manual method of (in JavaScript code):

但是,在这种情况下,这取决于。如果集合未分片,您可以使用renameCollectionhttp://docs.mongodb.org/manual/reference/command/renameCollection/),或者您可以使用更手动的方法(在 JavaScript 代码中):

db.collection1.drop(); // Drop entire other collection
db.collection1_backup.find().forEach(function(doc){
   db.collection1.insert(doc); // start to replace
});

Those are the most common methods of doing this.

这些是最常用的方法。

回答by Marcin Wasiluk

also usefull: to export collection to json file

也有用:将集合导出到 json 文件

mongoexport --collection collection1_backup --out collection1.json

to import collection from json file

从json文件导入集合

mongoimport --db test --collection collection1 --file collection1.json

to import single collection from backup/dump file one need to convert *.bson file to *.json by using

从备份/转储文件中导入单个集合需要将 *.bson 文件转换为 *.json 使用

bsondump collection1_backup.bson > collection1_backup.json

回答by Aman

This can be done using simple command:

这可以使用简单的命令来完成:

db.collection1_backup.aggregate([ { $match: {} }, { $out: "collection1" } ])

This command will remove all the documents of collection1 and then make a clone of collection1_backup in collection1.

此命令将删除 collection1 的所有文档,然后在 collection1 中创建 collection1_backup 的克隆。

Generic Command would be

通用命令将是

db.<SOURCE_COLLECTION>.aggregate([ { $match: {} }, { $out: "<TARGET_COLLECTION>" } ])

If TARGET_COLLECTION does not exist, the above command will create it.

如果 TARGET_COLLECTION 不存在,上面的命令将创建它。

回答by Sowmay Jain

Better way would be to use .toArray()

更好的方法是使用 .toArray()

 db.collection1.drop(); // Drop entire other collection

 // creates an array which can be accessed from "data"
 db.collection1_backup.find().toArray(function(err, data) {

      // creates a collection and inserting the array at once
      db.collection1.insert(data);
 });

回答by Irshad Khan

You can use simple command to Backup MongoDB Collection

您可以使用简单的命令来备份 MongoDB 集合

db.sourceCollectionName.copyTo('targetCollectionName')

Your targetCollectionName must be in Single(') or Double(") Quote

您的 targetCollectionName 必须在 Single(') 或 Double(") 引号中

Note:

笔记:

The db.collection.copyTo() method uses the eval command internally. As a result, the db.collection.copyTo() operation takes a global lock that blocks all other read and write operations until the db.collection.copyTo() completes.

db.collection.copyTo() 方法在内部使用 eval 命令。因此, db.collection.copyTo() 操作采用全局锁,阻止所有其他读写操作,直到 db.collection.copyTo() 完成。

回答by Bala Venkatesh Koripalli

Using Java Driver

使用 Java 驱动程序

Try below one:

试试下面的一个:

public void copyTo(String db,String sourceCollection,String destinationCollection,int limit) throws        
UnknownHostException {

    MongoClient mongo = new MongoClient("localhost", 27017);
    DB database = mongo.getDB(db);
    DBCollection collection = database.getCollection(sourceCollection);
    DBCursor dbCursor = collection.find().limit(limit);
    List<DBObject> list =  dbCursor.toArray();
    DBCollection destination =  database.getCollection(destinationCollection);
    destination.insert(list, WriteConcern.NORMAL); //WRITE CONCERN is based on your requirment.

}

回答by Aslam Shaik

simply just do this.

只需这样做。

//drop collection1

//删除集合1

db.collection1.drop();

//copy data from collection1_backup to collection1

//将数据从collection1_backup复制到collection1

db.collection1.insert(db.collection1_backup.find({},{_id:0}).toArray());