如何在 MongoDB 中重命名集合?
要重命名 MongoDB 中的集合,可以使用 renameCollection() 方法。语法如下 −
db.yourOldCollectionName.renameCollection('yourNewCollectionName');为了理解上述语法,我们来列出数据库 sample 中的所有集合。查询如下 −
> 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
广告
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP