4

我有一个名为collection_One. 我想重命名集合,但失败了。mongotemplate给我看了:

在 admin 数据库上执行“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);
4

2 回答 2

3

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

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

字符串启动版本:2.1.4

于 2019-04-23T13:05:58.757 回答
0

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

于 2018-03-26T11:42:38.057 回答