如何将集合从一个MongoDB克隆到同一服务器上的另一个数据库 [英] How to clone a collection from one MongoDB to another on same server

查看:78
本文介绍了如何将集合从一个MongoDB克隆到同一服务器上的另一个数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Mongo 3.2.我的本地主机上有两个数据库,分别为 client1 client2 .现在, client1 包含一个名为 users 的集合.我想将此集合克隆到 client2 .

I'm using Mongo 3.2. I have two databases on my localhost named client1 and client2. Now client1 contains a collection named users. I want to clone this collection to client2.

我尝试过:-

使用client2

use client2

db.cloneCollection('localhost:27017','client1.users',{'active':true})

db.cloneCollection('localhost:27017', 'client1.users', { 'active' : true } )

这将输出

{"ok":0.0,"errmsg":不能从自身克隆"}

{ "ok" : 0.0, "errmsg" : "can't cloneCollection from self" }

是否禁止将集合从一个数据库克隆到同一服务器上的另一个数据库?

Is cloning a collection from one db to another on the same server prohibited?

推荐答案

几件事:

  1. 通常 cloneCollection 用于不同的mongo实例,但不能在同一实例上复制.
  2. 此外,如果您使用的是 v4.2 ,则应停止使用 copyDB & cloneCollection 导致它们被弃用 mongodump mongoimport .
  3. 我建议使用 mongodump & mongorestore :

  1. In general cloneCollection is used for different mongo instances but not to copy on same instances.
  2. Also if you're using v4.2 you should stop using copyDB & cloneCollection cause they're deprecated compatibility-with-v4.2 & start using mongodump and mongorestore or mongoexport & mongoimport.
  3. I would suggest to use mongodump & mongorestore :

  1. 原因 mongodump 将保留MongoDB的数据类型,即; bson 类型.
  2. mongodump 创建一个二进制文件,其中 mongoexport 会将 bson 转换为 json &再次 mongoimport 会在编写时将 json 转换为 bson ,这就是它们运行缓慢的原因.您可以使用mongoexport&mongoimport当您想以可视方式分析您的收藏数据或将 json 数据用于其他任何目的时.
  1. Cause mongodump would preserve MongoDB's data types i.e.; bson types.
  2. mongodump creates a binary where as mongoexport would convert bson to json & again mongoimport will convert json to bson while writing, which is why they're slow. You can use mongoexport & mongoimport when you wanted to analyze your collections data visually or use json data for any other purpose.

  • 您可以在shell中的脚本下面运行

  • You can run below script in shell

    declare - a collections = ("collectionName1" "collectionName2")
    for i in "${collections[@]}"
    do
    echo "$i"
        mongodump --host "All-shards" --username=uname --password password --ssl --authenticationDatabase admin --db dbname --collection "$i"
    
        mongorestore --host=host-shard-name --port=27017 --username=uname --password=psswrd --ssl --authenticationDatabase=admin --db=dbname --collection= "$i" ./dump/dbName/"$i".bson;
    done
    

  • 要使用 mongodump ,必须对正在运行的mongod或mongos实例运行mongodump.因此,这些命令正在运行,期望mongo已正确安装和安装.路径设置很好,如果没有,您可以导航到mongo文件夹&像 ./mongodump &一样运行 ./mongorestore .如果要备份多个集合,以上脚本将很有用.您需要在脚本中指定一些内容,例如:

    To use mongodump, you must run mongodump against a running mongod or mongos instance. So these commands are being run expecting mongo is properly installed & path setup is good, if not you can navigate to mongo folder & run like ./mongodump & ./mongorestore. Above script will be useful if you wanted to backup multiple collections, You need specify few things in script like :

    1. mongodump-主机所有分片" ->如果您的MongoDB是副本集,则需要在此指定所有分片;否则,您可以指定 localhost:27017.

    1. mongodump--host "All-shards" -> Here you need to specify all shards if your MongoDB is a replica set, if not you can specify localhost:27017.

    mongorestore --host = host-shard-name ->您必须指定一个副本集的分片,否则您的 localhost ,这里没有什么可以是可选的-ssl -username -password .

    mongorestore --host=host-shard-name -> You've to specify one shard of replica set, else your localhost, Few things here can be optional --ssl, --username, --password.

    注意::MongoDB v3.2 太旧了&在基于云的MongoDB服务 Mongo-atlas 中,它已经到了生命周期的尽头,所以请升级尽快.如果您要寻找免费的mongo实例或从MongoDB开始-您可以尝试使用地图集.

    Note : MongoDB v3.2 is so old & in cloud based MongoDB service Mongo-atlas it has already reached it's end of lifecycle, So please upgrade asap. If you're looking for a free mongo instance or starting with MongoDB - you can try atlas.

    这篇关于如何将集合从一个MongoDB克隆到同一服务器上的另一个数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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