删除名称与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 50多岁生日简短祝福语
13 安徽疫情祝福语简短英语
14 农民朋友发财祝福语简短
15 对生活祝福语简短精辟
16 搬家词简短祝福语朋友
17 女神结婚快乐祝福语简短
18 文学短句祝福语大全简短