从一个集合到另一个的mongo复制(在同一数据库上) [英] mongo copy from one collection to another (on the same db)
问题描述
我有一个名为test
的mongo数据库,并且在该数据库中有两个集合collection1
和collection1_backup
.
如何用collection1_backup
中的数据替换collection1
的内容.
I have got mongo db called test
and in this db two collections collection1
and collection1_backup
.
How to replace content of collection1
with data from collection1_backup
.
推荐答案
执行此操作的最佳方法(考虑集合的名称以_backup
结尾)可能是使用mongorestore:
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/
但是在这种情况下要视情况而定.如果未对集合进行分片,则可以使用renameCollection
( http://docs.mongodb. org/manual/reference/command/renameCollection/),也可以使用更手动的方法(在JavaScript代码中):
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):
db.collection1.drop(); // Drop entire other collection
db.collection1_backup.find().forEach(function(doc){
db.collection1.insert(doc); // start to replace
});
这些是最常用的方法.
这篇关于从一个集合到另一个的mongo复制(在同一数据库上)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!