Mongotemplate无法重命名集合

时间:2015-12-08 17:10:17

标签: mongotemplate

我有一个名为collection_One的集合。我想重命名该集合,但失败了。 mongotemplate告诉我:

  

执行' renameCollection'管理数据库上的命令"

如果我跑:

db.collection_one.renameCollection("collection_one","collection_two"); 在mongo shell中,它工作正常。

如何通过mongotemplate重命名集合?

这是我尝试的代码:

BasicDBObject basicObject = new BasicDBObject();
basicObject.append("renameCollection","collection_one");
basicObject.append("to","collection_two");
mongoTemplate.executeCommand(basicObject);

2 个答案:

答案 0 :(得分:1)

如果您使用的是spring的mongo模板,请尝试以下方法: mongoTemplate.getCollection(currName).rename(newName);

答案 1 :(得分:0)

String dbName = default; // mongo数据库名称

MongoNamespace mongoNamespace=new MongoNamespace(dbName,"collection_two");
mongoTemplate.getCollection("collection_one").renameCollection(mongoNamespace);

字符串启动版本:2.1.4

相关问题