Computer >> 컴퓨터 >  >> 프로그램 작성 >> MongoDB

이름이 MongoDB의 문자열과 일치하는 모든 컬렉션 제거

<시간/>

이름이 문자열과 일치하는 모든 컬렉션을 제거하려면 몇 가지 단계를 수행할 수 있습니다. for 루프를 사용하여 모든 컬렉션을 반복하고 특정 문자열이 있는 특정 컬렉션 이름을 찾습니다. 그런 다음 drop 메서드를 사용하여 모든 컬렉션을 제거합니다.

데이터베이스 "샘플"을 사용하고 있다고 가정해 보겠습니다. 샘플 데이터베이스의 컬렉션은 다음과 같습니다.

> 컬렉션 표시

그러면 다음과 같은 출력이 생성됩니다.

arraySizeErrorDemobasicInformationDemocopyThisCollectionToSampleDatabaseDemodeleteAllRecordsDemodeleteDocumentsdeleteDocumentsDemodeleteMultipleIdsDemodeleteSomeInformationdocumentWithAParticularFieldValueDemoemployeefindListOfIdsDemofindMimimumElementInArrayDemofindSubstringgetAllRecordsFromSourceCollectionDemogetElementWithMaxIdDemoinsertDocumentWithDateDemointernalArraySizeDemolargestDocumentDemomakingStudentInformationCloneoppositeAddToSetDemoprettyDemoreturnOnlyUniqueValuesDemoselectWhereInDemosourceCollectionstudentInformationsumOfValueDemosumTwoFieldsDemotruncateDemoupdateInformationuserInformation

이제 "delete" 문자열과 일치하는 모든 컬렉션 이름을 제거합니다. 다음은 쿼리입니다.

> var allCollectionName =db.getCollectionNames();> for(var j=0, colLength =allCollectionName.length; j 

그러면 다음과 같은 출력이 생성됩니다.

사실

이제 모든 컬렉션이 샘플 데이터베이스에서 성공적으로 제거되었기 때문에 이름이 "delete"인 컬렉션이 없음을 알 수 있습니다.

이제 모든 컬렉션 이름을 확인하겠습니다. 다음은 쿼리입니다.

> 컬렉션 표시

그러면 다음과 같은 출력이 생성됩니다.

arraySizeErrorDemobasicInformationDemocopyThisCollectionToSampleDatabaseDemodocumentWithAParticularFieldValueDemoemployeefindListOfIdsDemofindMimimumElementInArrayDemofindSubstringgetAllRecordsFromSourceCollectionDemogetElementWithMaxIdDemoinsertDocumentWithDateDemointernalArraySizeDemolargestDocumentDemomakingStudentInformationCloneoppositeAddToSetDemoprettyDemoreturnOnlyUniqueValuesDemoselectWhereInDemosourceCollectionstudentInformationsumOfValueDemosumTwoFieldsDemotruncateDemoupdateInformationuserInformation