从一个集合到另一个的mongo复制(在同一数据库上) [英] mongo copy from one collection to another (on the same db)

查看:75
本文介绍了从一个集合到另一个的mongo复制(在同一数据库上)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为test的mongo数据库,并且在该数据库中有两个集合collection1collection1_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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆