在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
1. db.cloneCollection() function (from, collection, query) { assert( isString(from) && from.length ); assert( isString(collection) && collection.length ); collection = this._name + "." + collection; query = query || {}; return this._dbCommand( { cloneCollection:collection, from:from, query:query } ); } 参数: from string 包含需要复制的表的mongodb实例主机名 collection string 数据实例中需要复制的表名,该命令只可以复制远程mongodb实例上相同数据库名称的表 query document 可选的选项。标准的查询语句过滤掉不需要的文档 db.cloneCollection()不允许通过mongos来复制表,只能通过mongod实例来操作。 { "_id" : ObjectId("53687d9df433cf04b788c6d1"), "name" : "dog" } { "_id" : ObjectId("53687ff1f433cf04b788c6d2"), "name" : "cat" } { "_id" : ObjectId("53687ff4f433cf04b788c6d3"), "name" : "tiger" } 本地mongod实例mydb库,复制远程主机的bar集合中满足查询条件的文档: db.cloneCollection("192.168.11.52", "bar", {"name" : "tiger"}) db.bar.find(); { "_id" : ObjectId("53687ff4f433cf04b788c6d3"), "name" : "tiger" } 2. db.cloneDatabase() hostname string 包含需要复制的数据库的mongodb实例主机名 db.cloneDatabase是clone数据库命令的一个外在体现。 function (from) { assert( isString(from) && from.length ); return this._dbCommand( { clone: from } ); } 示例: use mydb db.dropDatabase(); db.cloneDatabase("192.168.11.52"); 3. db.copyDatabase() db.copyDatabase是copydb数据库命令的一个外在体现。 function (fromdb, todb, fromhost, username, password) { assert( isString(fromdb) && fromdb.length ); assert( isString(todb) && todb.length ); fromhost = fromhost || ""; if ( username && password ) { var n = this._adminCommand( { copydbgetnonce : 1, fromhost:fromhost } ); return this._adminCommand( { copydb:1, fromhost:fromhost, fromdb:fromdb, todb:todb, username:username, nonce:n.nonce, key:this.__pwHash( n.nonce, userna me, password ) } ); } else { return this._adminCommand( { copydb:1, fromhost:fromhost, fromdb:fromdb, todb:todb } ); } } 参数: fromdb string 源数据库名称 todb string 目标数据库名称 fromhost string 可选项,源数据库的主机名。如果是同一主机,忽略该选项 username string 可选项,源主机名用户名 password string 可选项,源主机名用户名对应密码 属性: { resource: { db: "mySourceDB", collection: "system.indexes" }, actions: [ "find" ] } { resource: { db: "mySourceDB", collection: "system.namespaces" }, actions: [ "find" ] } (2)如果源主机数据库是admin,必须确保拥有以下权限: { resource: { db: "admin", collection: "" }, actions: [ "find" ] } { resource: { db: "admin", collection: "system.js" }, actions: [ "find" ] } { resource: { db: "admin", collection: "system.users" }, actions: [ "find" ] } { resource: { db: "admin", collection: "system.roles" }, actions: [ "find" ] } { resource: { db: "admin", collection: "system.version" }, actions: [ "find" ] } 如果源主机是一台远程主机,必须确保拥有以下权限: { resource: { db: "admin", collection: "system.indexes" }, actions: [ "find" ] } { resource: { db: "admin", collection: "system.namespaces" }, actions: [ "find" ] } (3)源数据库在远程主机 { resource: { db: "myTargetDB", collection: "" }, actions: [ "insert", "createIndex" ] } { resource: { db: "myTargetDB", collection: "system.js" }, actions: [ "insert" ] } B、如果目标主机数据库是admin,必须确保拥有以下权限: resource: { db: "myTargetDB", collection: "" }, actions: [ "insert", "createIndex" ] }, { resource: { db: "myTargetDB", collection: "system.js" }, actions: [ "insert" ] }, { resource: { db: "myTargetDB", collection: "system.users" }, actions: [ "insert" ] }, { resource: { db: "myTargetDB", collection: "system.roles" }, actions: [ "insert" ] }, { resource: { db: "myTargetDB", collection: "system.version" }, actions: [ "insert" ] } 示例: db.copyDatabase("mydb", "newmydb", "192.168.11.52"); 4. cloneCollection { cloneCollection: "<namespace>", from: "<hostname>", query: { <query> } } cloneCollection拥有以下的域值: cloneCollection string 集合的命名空间,命名空间包含了数据库名和集合名的组合 from string 指定远程主机名和可选的端口号 query document 可选的,过滤选项 示例: { "_id" : ObjectId("53687d9df433cf04b788c6d1"), "name" : "dog" } { "_id" : ObjectId("53687ff1f433cf04b788c6d2"), "name" : "cat" } { "_id" : ObjectId("53687ff4f433cf04b788c6d3"), "name" : "tiger" } 本地mongod实例: db.runCommand({cloneCollection : "mydb.bar", from : "192.168.11.52:27017", query : {"name" : "tiger"}}) use mydb db.bar.find() { "_id" : ObjectId("53687ff4f433cf04b788c6d3"), "name" : "tiger" } cloneCollectionAsCapped可以利用数据库中存在的非cpped集合创建出一个新的capped集合,操作对原来的集合没有副作用。 db.runCommand({cloneCollectionAsCapped : "bar", toCollection : "barone", size : 100}) db.barone.isCapped(); true 5. clone { clone: "db1.example.net:27017" } 需要注意的几点: 6. copydb { copydb: 1, fromhost: <hostname>, fromdb: <database>, todb: <database>, slaveOk: <bool>, username: <username>, nonce: <nonce>, key: <key> } 选项: fromhost string 运行mongodb实例的远程源主机,如果是本地可以忽略 fromdb string 源数据库名称 todb string 目标数据库名称 slaveOk boolean 可选的,设置为true,允许从从库复制库 username string 可选的,远程主机的用户名。 nonce string 可选的,远程主机的共享密钥 key string 可选的,远程主机的认证密码哈希 属性: { resource: { db: "mySourceDB", collection: "" }, actions: [ "find" ] } { resource: { db: "mySourceDB", collection: "system.js" }, actions: [ "find" ] } 如果源主机是一台远程主机,必须确保拥有以下权限: { resource: { db: "mySourceDB", collection: "system.indexes" }, actions: [ "find" ] } { resource: { db: "mySourceDB", collection: "system.namespaces" }, actions: [ "find" ] } (2)如果源主机数据库是admin,必须确保拥有以下权限: { resource: { db: "admin", collection: "" }, actions: [ "find" ] } { resource: { db: "admin", collection: "system.js" }, actions: [ "find" ] } { resource: { db: "admin", collection: "system.users" }, actions: [ "find" ] } { resource: { db: "admin", collection: "system.roles" }, actions: [ "find" ] } { resource: { db: "admin", collection: "system.version" }, actions: [ "find" ] } 如果源主机是一台远程主机,必须确保拥有以下权限: { resource: { db: "admin", collection: "system.indexes" }, actions: [ "find" ] } { resource: { db: "admin", collection: "system.namespaces" }, actions: [ "find" ] } (3)源数据库在远程主机 { resource: { db: "myTargetDB", collection: "" }, actions: [ "insert", "createIndex" ] } { resource: { db: "myTargetDB", collection: "system.js" }, actions: [ "insert" ] } B、如果目标主机数据库是admin,必须确保拥有以下权限: resource: { db: "myTargetDB", collection: "" }, actions: [ "insert", "createIndex" ] }, { resource: { db: "myTargetDB", collection: "system.js" }, actions: [ "insert" ] }, { resource: { db: "myTargetDB", collection: "system.users" }, actions: [ "insert" ] }, { resource: { db: "myTargetDB", collection: "system.roles" }, actions: [ "insert" ] }, { resource: { db: "myTargetDB", collection: "system.version" }, actions: [ "insert" ] } 认证: use admin mynonce = db.runCommand( { copydbgetnonce : 1, fromhost: <hostname> } ).nonce 如果直接在远程主机运行copydbgetnonce命令,可以忽略fromhost选项。 hex_md5(mynonce + username + hex_md5(username + ":mongo:" + password)) 副本集:设置slaveOk为true,可以在从节点运行copydb。 实例: (2)从远程主机复制的copydb db._adminCommand({ copydb : 1, fromdb : "mydb", todb : "mydbtwo", formhost : "192.168.11.52" }) { "ok" : 1 } (3)从需要安全验证的远程主机复制的copydb use admin mynonce = db.runCommand( { copydbgetnonce : 1, fromhost: "192.168.11.51:27017" } ).nonce mykey = hex_md5(mynonce + "test" + hex_md5("test" + ":mongo:" + "caoqing")) db._adminCommand({ copydb: 1, fromdb: "mydb", todb: "mydbthree", fromhost: "192.168.11.51", username: "test", nonce: mynonce, key: mykey }) { "ok" : 1 } |
请发表评论