Mongo副本收集和用户权限 [英] Mongo Copy Collection and User Permissions

查看:79
本文介绍了Mongo副本收集和用户权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过外壳复制一个集合,但是尽管具有读/写权限,但我遇到了未授权"错误.具体来说:

I am trying to copy a collection through the shell, but I am getting an 'unauthorized' error, despite having read/write permissions. Specifically:

db.createCollection("ITISAFAKE")

{ "ok" : 1 }

db.ITISAFAKE.insert({"Is it a fake?": true})

db.ITISAFAKE.find()

{ "_id" : ObjectId("52d5e51d4bb0851f985f69d8"), "Is it a fake?" : true }

db.ITISAFAKE.drop()

true

是的,效果很好.但是:

Yay that works fine. However:

db.createCollection("FAKE") 

{ "ok" : 1 }

db.FAKE.insert({senator: true})

db.FAKE.copyTo("FAKERY")

Tue Jan 14 17:36:26.188 { "ok" : 0, "errmsg" : "unauthorized" } at src/mongo/shell/db.js:571

因此,我可以通过逐字逐条地逐个记录地复制内容来解决此问题,但这似乎很愚蠢.我可以摆弄用户权限等,但是我怎么...实际上只是复制内容?

So, I could solve this problem by literally copying things over record by record, but that seems dumb. I can fiddle with user permissions etc, but how do I ... actually just copy things?

推荐答案

每个 collection.copyTo() 文档中,此shell帮助程序使用服务器端JavaScript将所有文档从集合复制到newCollection中".

Per the collection.copyTo() documentation in MongoDB 2.4, this shell helper "copies all documents from collection into newCollection using server-side JavaScript".

我希望您收到unauthorized错误的原因是因为服务器端 db.eval()命令需要完全管理员MongoDB 2.4中的权限(admin数据库上的多个角色).

I expect the reason you are getting an unauthorized error is because the server-side db.eval() command requires full admin permissions in MongoDB 2.4 (multiple roles on the admin database).

如果在不使用()的情况下调用copyTo(),则实际上可以在mongo shell中看到此帮助程序的源代码:

You can actually see the source for this helper in the mongo shell if you invoke copyTo() without the ():

> db.test.copyTo
function ( newName ){
    return this.getDB().eval(
        function( collName , newName ){
            var from = db[collName];
            var to = db[newName];
            to.ensureIndex( { _id : 1 } );
            var count = 0;

            var cursor = from.find();
            while ( cursor.hasNext() ){
                var o = cursor.next();
                count++;
                to.save( o );
            }

            return count;
        } , this.getName() , newName
    );
}

由于使用服务器端db.eval(),因此有关于copyTo()使用的几个重要警告,包括有关类型保真度和锁定问题的警告.我相信此帮助程序仅应在具有更多开放权限的开发环境中使用,而不是在生产环境中使用.

Due to the use of server-side db.eval(), there are several significant warnings on copyTo() usage including cautions on type fidelity and locking issues. I believe this helper is only intended to be used in development environments with more open permissions, rather than a production environment.

因此,我可以通过逐个记录逐个复制内容来解决此问题

So, I could solve this problem by literally copying things over record by record

这不是一个完全疯狂的选择:).这是mongo外壳的简单一线式(适当地替换sourcetarget集合):

That's not an entirely crazy option :). Here is a simple one-liner for the mongo shell (replace source and target collections as appropriate):

db.source.find().forEach( function(d) {db.target.insert(d)});

如果您担心网络带宽,可以从您的MongoDB服务器本地的mongo shell运行它.

If you are concerned about network bandwidth you can run this from a mongo shell which is local to your MongoDB server.

一种更好的复制集合的方法(尽管不是直接从mongo外壳中复制)是使用mongodump& mongorestore:

A better approach to copying a collection (although not directly from the mongo shell) would be to use mongodump & mongorestore:

$ mongodump -d test -c source 
$ mongorestore -d test -c target dump/test/source.bson                                                                                                                                            

这篇关于Mongo副本收集和用户权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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