删除名称与MongoDB中的字符串匹配的所有集合
要删除名称与字符串匹配的所有集合,可以执行一些步骤。使用for循环遍历所有集合,并使用特定字符串查找该特定集合名称。之后,使用drop方法删除所有集合。
假设我们正在使用数据库“样本”。样本数据库中的集合如下
> show collections;
这将产生以下输出
arraySizeErrorDemo basicInformationDemo copyThisCollectionToSampleDatabaseDemo deleteAllRecordsDemo deleteDocuments deleteDocumentsDemo deleteMultipleIdsDemo deleteSomeInformation documentWithAParticularFieldValueDemo employee findListOfIdsDemo findMimimumElementInArrayDemo findSubstring getAllRecordsFromSourceCollectionDemo getElementWithMaxIdDemo insertDocumentWithDateDemo internalArraySizeDemo largestDocumentDemo makingStudentInformationClone oppositeAddToSetDemo prettyDemo returnOnlyUniqueValuesDemo selectWhereInDemo sourceCollection studentInformation sumOfValueDemo sumTwoFieldsDemo truncateDemo updateInformation userInformation
现在,删除所有与字符串“delete”匹配的集合名称。以下是查询
> var allCollectionName = db.getCollectionNames();
> for(var j= 0, colLength = allCollectionName.length; j< colLength ; j++){
... var colName = allCollectionName[j];
... if(colName.indexOf('delete') == 0){
... db[colName].drop()
... }
... }这将产生以下输出
True
现在您可以看到没有名称为“delete”的集合,因为已成功从示例数据库中删除了所有集合。
现在让我们检查所有集合名称。以下是查询
> show collections;
这将产生以下输出
arraySizeErrorDemo basicInformationDemo copyThisCollectionToSampleDatabaseDemo documentWithAParticularFieldValueDemo employee findListOfIdsDemo findMimimumElementInArrayDemo findSubstring getAllRecordsFromSourceCollectionDemo getElementWithMaxIdDemo insertDocumentWithDateDemo internalArraySizeDemo largestDocumentDemo makingStudentInformationClone oppositeAddToSetDemo prettyDemo returnOnlyUniqueValuesDemo selectWhereInDemo sourceCollection studentInformation sumOfValueDemo sumTwoFieldsDemo truncateDemo updateInformation userInformation
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短