2

我正在使用 mongohq 沙盒计划。在命令提示符下,

db["oldCollectionName"].renameCollection("newCollectionName", true)

在不使用管理数据库的情况下工作正常。

但是,当我在 Java 中执行此操作时,我得到了“未经授权”的异常:

oldCollection.rename(newCollectionName);

由于我使用的是 mongohq 沙盒计划,因此我无权访问管理数据库。有没有办法在不创建新集合、复制所有文档和删除旧集合的情况下重命名此集合?

4

1 回答 1

0

In Java and using Jongo you can do the following:

MongoCollection col = new Jongo(DbConfigurer.getDB()).getCollection("CODE");
col.getDBCollection().rename("CODE45", true);

I've just tested and it works.

Now using the 'runCommand' (same than using db.command) in the following example:

DB db = ....getDB();
Jongo jongo = new Jongo(db);
jongo.runCommand("{ renameCollection : 'OLD_NAME', to: 'NEW_NAME', dropTarget: true}");

I obtain the same error you have.

I read from some documentation you have to connect first to admin database to process some of the command not allowed, so i did the same but with "admin" db, and i obtain the following error stack:

{ "serverUsed" : "localhost/127.0.0.1:27017" , 
"errmsg" : "exception: invalid collection name: NEW_NAME" , "code" : 15967 , "ok" : 0.0}

Strange to have such different behaviors...

于 2013-05-08T14:48:56.463 回答