要在MongoDB中重命名集合,可以使用renameCollection()
方法。语法如下-
db.yourOldCollectionName.renameCollection('yourNewCollectionName');
为了理解上述语法,让我们列出数据库样本中的所有集合。查询如下-
> use sample; switched to db sample > show collections;
以下是输出-
copyThisCollectionToSampleDatabaseDemo deleteDocuments deleteDocumentsDemo employee informationAboutDelete internalArraySizeDemo prettyDemo selectWhereInDemo sourceCollection updateInformation userInformation
现在,将集合名称“ informationAboutDelete”更改为“ deleteSomeInformation”。查询如下以更改集合名称。
> db.informationAboutDelete.renameCollection('deleteSomeInformation'); { "ok" : 1 }
这是查询以检查集合名称已重命名为'deleteSomeInformation'-
> show collections;
以下是输出-
copyThisCollectionToSampleDatabaseDemo deleteDocuments deleteDocumentsDemo deleteSomeInformation employee internalArraySizeDemo prettyDemo selectWhereInDemo sourceCollection updateInformation userInformation