如何在 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
廣告